How can I use a custom bitmap for the "you are here" point in MyLocationOverlay?

I poured the documents and could not understand it. Is it possible?

See this

+2
source share
2 answers

It seems that the correct mechanism for this is to expand MyLocationOverlay, and then override the drawMyLocation () method.

The next line uses an arrow showing where you are and in which direction you are:

 package com.example; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Point; import android.location.Location; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapView; import com.google.android.maps.MyLocationOverlay; public class MyCustomLocationOverlay extends MyLocationOverlay { private Context mContext; private float mOrientation; public MyCustomLocationOverlay(Context context, MapView mapView) { super(context, mapView); mContext = context; } @Override protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) { // translate the GeoPoint to screen pixels Point screenPts = mapView.getProjection().toPixels(myLocation, null); // create a rotated copy of the marker Bitmap arrowBitmap = BitmapFactory.decodeResource( mContext.getResources(), R.drawable.arrow_green); Matrix matrix = new Matrix(); matrix.postRotate(mOrientation); Bitmap rotatedBmp = Bitmap.createBitmap( arrowBitmap, 0, 0, arrowBitmap.getWidth(), arrowBitmap.getHeight(), matrix, true ); // add the rotated marker to the canvas canvas.drawBitmap( rotatedBmp, screenPts.x - (rotatedBmp.getWidth() / 2), screenPts.y - (rotatedBmp.getHeight() / 2), null ); } public void setOrientation(float newOrientation) { mOrientation = newOrientation; } } 

code>

+14
source

I made a few changes to the previuos code to make it work correctly, because the arrow was pointing in the wrong direction and spinning in the opposite direction.

I changed

 matrix.postRotate(mOrientation); 

for

 matrix.postRotate(this.getRotation()); 

and added to the end:

 mapView.postInvalidate(); 

to redraw the arrow when it changes.

+2
source

Source: https://habr.com/ru/post/1347807/


All Articles