how do i change the default blue animated marker for MyLocationOverlay in google maps?
2 Answers
Thx @CommonsWare, you led me in the right direction. Spent quite a wile twiddling with this. The Javadoc on http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MyLocationOverlay.html is just wrong (or outdated) and messes with your brain where it says:
drawMyLocation
Also, if the user's position moves near the edge of the screen, and we've been given a MapController in our constructor, we'll scroll to recenter the new reading.
This is completely wrong. First, there is no such constructor in the class. Second of all, drawMyLocation only gets called when the current Location is within the viewbounds, or the screen is moved. So if you receive a new location and you don't see the marker at this moment, it won't be redrawn. Which is a good thing in general, but a bad thing if you want to implement mc.animateTo in the method to keep the location centered.
Third of all, you shouldn't need to pass a MapController, to enable currentLocation, because you already have the mapView and get the controller from it..
So I ended up writing my own Class that keeps centering on the current location once the location reaches the border of the mapView or is 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;
}
}

- 4,973
- 1
- 43
- 52
Step #1: Create a subclass of MyLocationOverlay
.
Step #2: Override drawMyLocation()
and draw the marker however you like. Bear in mind that this method not only draws the marker, but "if the user's position moves near the edge of the screen, and we've been given a MapController
in our constructor, we'll scroll to recenter the new reading".

- 986,068
- 189
- 2,389
- 2,491
-
i just want to make it green instead of blue. i already found the resources in android (the animation xml resource and the 4 images). the part of changing the color is easy. but as the programing goes i want the exact same behavior. is there anywhere i can find the code for the drawMyLocationMethod() ?? – DArkO Nov 15 '10 at 23:53
-
1@DArkO: Sorry, but the Google APIs add-on is not open source. – CommonsWare Nov 16 '10 at 00:09
-
i had 1 more idea on how to make this. can i replace the resources in the adroid SDK with my version(everything like the names, dimensions stays the same)? – DArkO Nov 16 '10 at 14:48
-
1@DArkO: Only by creating your own firmware. – CommonsWare Nov 16 '10 at 14:53
-
Hmn it says that in the Doc. But i've never found a Constructor that allows to pass a MapController. Am I missing something? – icyerasor Mar 05 '11 at 21:21
-
@icyerasor: You need to write the constructor, as part of step #1 above. – CommonsWare Mar 05 '11 at 22:18