MapView显示很多GeoPoints之间的路由

时间:2012-12-10 11:39:59

标签: android android-mapview routes

我有一个应用程序,其中给予用户很多积分(100或甚至更多),他必须亲自去那些点并“登记”。他们必须按特定顺序去那些点,所以我需要在MapView中显示一条路径,它通过所有这些点。

我已经阅读了很多关于在两点之间获得路线的信息,但我找不到任何有关绘制具有大量分数的复杂路线的信息。这种行为可能吗?

2 个答案:

答案 0 :(得分:1)

public class RouteOverlay extends Overlay {
    private GeoPoint gp1;
    private GeoPoint gp2;
    private int color;

public RouteOverlay(GeoPoint gp1, GeoPoint gp2, int color) {
        this.gp1 = gp1;
        this.gp2 = gp2;
        this.color = color;
    }
Now all that's left now for our Overlay is to override the draw() method and draw the line as we need it:

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    Projection projection = mapView.getProjection();
    Paint paint = new Paint();
    Point point = new Point();
    projection.toPixels(gp1, point);
    paint.setColor(color);
    Point point2 = new Point();
    projection.toPixels(gp2, point2);
    paint.setStrokeWidth(5);
    paint.setAlpha(120);
    canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
    super.draw(canvas, mapView, shadow);
}
Back in the Activity, just iterate over the GeoPoints that you got from google maps and add each of them to the MapView:

private void drawPath(List geoPoints, int color) {
   List overlays = mapView.getOverlays();

   for (int i = 1; i < geoPoints.size(); i++) {
    overlays.add(new RouteOverlay(geoPoints.get(i - 1), geoPoints.get(i), color));
   }
}

答案 1 :(得分:0)

尝试这样的事情

if(DataSources.ActivitiesList.length >0)
{
  String address = "http://maps.google.com/maps?daddr=" +    DataSources.ActivitiesList[0].SiteLatitude.toString() + "," + DataSources.ActivitiesList[0].SiteLongitude.toString();
for (int i= 1 ;i <  DataSources.ActivitiesList.length ; i++) 
{
    if(DataSources.ActivitiesList[i].SiteLatitude != null)
        address += "+to:" + DataSources.ActivitiesList[i].SiteLatitude + "," + DataSources.ActivitiesList[i].SiteLongitude;
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(address));
    startActivity(intent);
    break;      
}