MyLocationOverlay change marker

How to change the blue animated default marker for MyLocationOverlay on google maps?

+4
source share
2 answers

Step # 1: Subclass MyLocationOverlay .

Step # 2: override drawMyLocation() and draw a marker as you like. Keep in mind that this method not only draws a marker, but "if the user’s position moves closer to the edge of the screen and we are provided with MapController in our constructor, we will scroll to enable new reading."

+6
source

thanks @CommonsWare, you brought me in the right direction. Spent a rather bold turn with this. Javadoc at http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MyLocationOverlay.html is just wrong (or out of date) and gets confused with your brain where it says :

drawMyLocation

In addition, if the user's position moves near the edge of the screen, and we were provided with MapController in our constructor, we will proceed to re-record the new reading.

This is completely wrong. Firstly, there is no such constructor in the class. Secondly, drawMyLocation receives a call only when the current location is within the viewport or the screen moves. Therefore, if you get a new location, and you do not see the marker at this moment, it will not be redrawn. This is good, in general, but bad if you want to implement mc.animateTo in a method to maintain location orientation.

Thirdly, you do not need to pass the MapController to enable currentLocation, because you already have a mapView and get a controller from it.

So, I ended up writing my own class, which focused on the current location as soon as the location reaches the border of the mapView or off screen:

 public class CurrentLocationOverlay extends MyLocationOverlay { // TODO: use dynamic calculation? private final static int PADDING_ACTIVE_ZOOM = 50; private MapController mc; private Bitmap marker; private Point currentPoint = new Point(); private boolean centerOnCurrentLocation = true; private int height; private int width; /** * By default this CurrentLocationOverlay will center on the current location, if the currentLocation is near the * edge, or off the screen. To dynamically enable/disable this, use {@link #setCenterOnCurrentLocation(boolean)}. * * @param context * @param mapView */ public CurrentLocationOverlay(Context context, MapView mapView) { super(context, mapView); this.mc = mapView.getController(); this.marker = BitmapFactory.decodeResource(context.getResources(), R.drawable.position); } @Override protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) { // TODO: find a better way to get height/width once the mapView is layed out correctly if (this.height == 0) { this.height = mapView.getHeight(); this.width = mapView.getWidth(); } mapView.getProjection().toPixels(myLocation, currentPoint); canvas.drawBitmap(marker, currentPoint.x, currentPoint.y - 40, null); } @Override public synchronized void onLocationChanged(Location location) { super.onLocationChanged(location); // only move to new position if enabled and we are in an border-area if (mc != null && centerOnCurrentLocation && inZoomActiveArea(currentPoint)) { mc.animateTo(getMyLocation()); } } private boolean inZoomActiveArea(Point currentPoint) { if ((currentPoint.x > PADDING_ACTIVE_ZOOM && currentPoint.x < width - PADDING_ACTIVE_ZOOM) && (currentPoint.y > PADDING_ACTIVE_ZOOM && currentPoint.y < height - PADDING_ACTIVE_ZOOM)) { return false; } return true; } public void setCenterOnCurrentLocation(boolean centerOnCurrentLocation) { this.centerOnCurrentLocation = centerOnCurrentLocation; } } 
+9
source

All Articles