Draw a path following your location on Google Maps for Android
I am working on an app for which I need a path to be drawn on a Google 开发者_运维知识库map as the location changes showing where the user has been. I have tried using the MyLocationOverlay class thinking I could just override the method that draws the location but I have been unsuccessful in determining what method to override. Also, it appears that MyLocationOverlay draws a new map every time the location is drawn. I am currently using an ItemizedOverlay and just having a dot added to the list every time the location changes. This works, and I get a dotted path as I walk but I would really like a solid path. Are there any suggestions?
I have also seen this post but I can't get it to work. Dont you need an overlay inorder to get it on the map?
I think the easiest way to do this would be to subclass the Overlay class, and then override the draw method. The draw method is pretty open ended and it shouldn't be too hard to draw a path. An example would look something like this:
public class PathOverlay extends Overlay{
private List<GeoPoint> gpoints;
public PathOverlay(List<GeoPoint> gpoints){
this.gpoints = gpoints;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
List<Point> mpoints = new ArrayList<Point>();
// Convert to a point that can be drawn on the map.
for(GeoPoint g : gpoints){
Point tpoint = new Point();
mapView.getProjection().toPixels(g, tpoint);
mpoints.add(tpoint);
}
Path path = new Path();
// Create a path from the points
path.moveTo(mpoints.get(0).x, mpoints.get(0).y);
for(Point p : mpoints){
path.lineTo(p.x, p.y);
}
Paint paint = new Paint();
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Style.STROKE);
// Draw to the map
canvas.drawPath(path,paint);
return true;
}
}
Objects of this class can then be added to the list returned by MapView.getOverlays() to be added to the map.
精彩评论