Android 使用两个 POI-s 在 Mapview 上绘制路线

发布于 2024-10-07 07:51:42 字数 33 浏览 4 评论 0原文

我如何在地图视图上绘制两个 poi-s 之间的路线?

How i can draw a route on a mapview between two poi-s?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2024-10-14 07:51:42

通过这些代码。根据您的要求修改代码

MapDirection.java:

public class MapDirection extends MapActivity{

  MapView mapview;
  MapRouteOverlay mapoverlay;
  Context _context;
  List<Overlay> maplistoverlay;
  Drawable drawable,drawable2;
  MapOverlay mapoverlay2,mapoverlay3;
  GeoPoint srcpoint,destpoint;
  Overlay overlayitem;
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);  
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.map_direction);
    RegisterActivities.registerActivity(this);
    mapview=(MapView)this.findViewById(R.id.mapview);

    callMap();
  }
  private void callMap() {


    srcpoint=new GeoPoint((int)(Data.src_lat_date*1E6),(int)(Data.src_long_data*1E6));
    maplistoverlay=mapview.getOverlays();
    drawable=this.getResources().getDrawable(R.drawable.green_a);
    mapoverlay2=new MapOverlay(drawable);
    OverlayItem overlayitem = new OverlayItem(srcpoint, "", "");
    mapoverlay2.addOverlay(overlayitem);
    maplistoverlay.add(mapoverlay2);

    destpoint=new GeoPoint((int)(Data.dest_lat_data*1E6),(int)(Data.dest_long_data*1E6));
    drawable2=this.getResources().getDrawable(R.drawable.green_b);
    mapoverlay3=new MapOverlay(drawable2);
    OverlayItem overlayitem3 = new OverlayItem(destpoint, "", "");
    mapoverlay3.addOverlay(overlayitem3);
    maplistoverlay.add(mapoverlay3);


    double dest_lat = Data.dest_lat_data;
    double dest_long = Data.dest_long_data;

    GeoPoint srcGeoPoint = new GeoPoint((int) (Data.src_lat_date* 1E6),
        (int) (Data.src_long_data * 1E6));
    GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
        (int) (dest_long * 1E6));

    DrawPath(srcGeoPoint, destGeoPoint, Color.BLUE, mapview);

    mapview.getController().animateTo(srcGeoPoint);
    mapview.getController().setZoom(13);
    //mapview.setStreetView(true);
    mapview.setBuiltInZoomControls(true);
    mapview.invalidate();


  }
  private void DrawPath(GeoPoint src, GeoPoint dest, int color,
      MapView mMapView01) {



    // connect to map web service
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.google.com/maps?f=d&hl=en");
    urlString.append("&saddr=");//from
    urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 ));
    urlString.append(",");
    urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 ));
    urlString.append("&daddr=");//to
    urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 ));
    urlString.append(",");
    urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 ));
    urlString.append("&ie=UTF8&0&om=0&output=kml");
    Log.d("xxx","URL="+urlString.toString());

    //System.out.println(urlString);
    // get the kml (XML) doc. And parse it to get the coordinates(direction route).
    Document doc = null;
    HttpURLConnection urlConnection= null;
    URL url = null;
    try
    {
      url = new URL(urlString.toString());
      urlConnection=(HttpURLConnection)url.openConnection();
      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(true);
      urlConnection.setDoInput(true);
      urlConnection.connect();

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      doc = db.parse(urlConnection.getInputStream());

      if(doc.getElementsByTagName("GeometryCollection").getLength()>0)
      {
        //String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
        String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ;
        Log.d("xxx","path="+ path);
        String [] pairs = path.split(" ");
        String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height
        // src
        GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
        //mMapView01.getOverlays().add(overlayitem);
        GeoPoint gp1;
        GeoPoint gp2 = startGP;
        for(int i=1;i<pairs.length;i++) // the last one would be crash
        {
          lngLat = pairs[i].split(",");
          gp1 = gp2;
          // watch out! For GeoPoint, first:latitude, second:longitude
          gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
          mMapView01.getOverlays().add(new MapRouteOverlay(gp1,gp2,2,color));
          Log.d("xxx","pair:" + pairs[i]);
        }
        //mMapView01.getOverlays().add(new MapRouteOverlay(dest,dest, 3)); // use the default color
      }
    }
    catch (MalformedURLException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    catch (ParserConfigurationException e)
    {
      e.printStackTrace();
    }
    catch (SAXException e)
    {
      e.printStackTrace();
    }


  }
  @Override
    protected boolean isRouteDisplayed() {
      // TODO Auto-generated method stub
      return false;
    }
}

MapRouteOverlay.java:

public class MapRouteOverlay extends Overlay {

  private GeoPoint gp1;
  private GeoPoint gp2;

  private int mode=0;
  private int defaultColor;




  public MapRouteOverlay(GeoPoint gp1,GeoPoint gp2,int mode) // GeoPoint is a int. (6E)
  {
    this.gp1 = gp1;
    this.gp2 = gp2;
    this.mode = mode;
    defaultColor = 999; // no defaultColor

  }

  public MapRouteOverlay(GeoPoint gp1,GeoPoint gp2,int mode, int defaultColor)
  {
    this.gp1 = gp1;
    this.gp2 = gp2;
    this.mode = mode;
    this.defaultColor = defaultColor;
  }

  public int getMode()
  {
    return mode;
  }

  public boolean draw
    (Canvas canvas, MapView mapView, boolean shadow, long when)
    {
      Projection projection = mapView.getProjection();
      if (shadow == false)
      {

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Point point = new Point();
        projection.toPixels(gp1, point);

        if(mode==2)
        {
          if(defaultColor==999)
            paint.setColor(Color.RED);
          else
            paint.setColor(defaultColor);
          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);
        }

      }
      return super.draw(canvas, mapView, shadow, when);
    }
}

MapOverlay.java:

public class MapOverlay extends ItemizedOverlay {
  private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

  public MapOverlay(Drawable _defaultMarker) {

    super(boundCenterBottom(_defaultMarker));
  }

  @Override
  protected OverlayItem createItem(int i) {
    return mOverlays.get(i);
  } 
  public void addOverlay(OverlayItem overlay) {
      mOverlays.add(overlay);
      populate();
  }

  @Override
  public int size() {
    return mOverlays.size();
  }

}

go through this codes. Modify the code as per ur requirement

MapDirection.java:

public class MapDirection extends MapActivity{

  MapView mapview;
  MapRouteOverlay mapoverlay;
  Context _context;
  List<Overlay> maplistoverlay;
  Drawable drawable,drawable2;
  MapOverlay mapoverlay2,mapoverlay3;
  GeoPoint srcpoint,destpoint;
  Overlay overlayitem;
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);  
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.map_direction);
    RegisterActivities.registerActivity(this);
    mapview=(MapView)this.findViewById(R.id.mapview);

    callMap();
  }
  private void callMap() {


    srcpoint=new GeoPoint((int)(Data.src_lat_date*1E6),(int)(Data.src_long_data*1E6));
    maplistoverlay=mapview.getOverlays();
    drawable=this.getResources().getDrawable(R.drawable.green_a);
    mapoverlay2=new MapOverlay(drawable);
    OverlayItem overlayitem = new OverlayItem(srcpoint, "", "");
    mapoverlay2.addOverlay(overlayitem);
    maplistoverlay.add(mapoverlay2);

    destpoint=new GeoPoint((int)(Data.dest_lat_data*1E6),(int)(Data.dest_long_data*1E6));
    drawable2=this.getResources().getDrawable(R.drawable.green_b);
    mapoverlay3=new MapOverlay(drawable2);
    OverlayItem overlayitem3 = new OverlayItem(destpoint, "", "");
    mapoverlay3.addOverlay(overlayitem3);
    maplistoverlay.add(mapoverlay3);


    double dest_lat = Data.dest_lat_data;
    double dest_long = Data.dest_long_data;

    GeoPoint srcGeoPoint = new GeoPoint((int) (Data.src_lat_date* 1E6),
        (int) (Data.src_long_data * 1E6));
    GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
        (int) (dest_long * 1E6));

    DrawPath(srcGeoPoint, destGeoPoint, Color.BLUE, mapview);

    mapview.getController().animateTo(srcGeoPoint);
    mapview.getController().setZoom(13);
    //mapview.setStreetView(true);
    mapview.setBuiltInZoomControls(true);
    mapview.invalidate();


  }
  private void DrawPath(GeoPoint src, GeoPoint dest, int color,
      MapView mMapView01) {



    // connect to map web service
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.google.com/maps?f=d&hl=en");
    urlString.append("&saddr=");//from
    urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 ));
    urlString.append(",");
    urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 ));
    urlString.append("&daddr=");//to
    urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 ));
    urlString.append(",");
    urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 ));
    urlString.append("&ie=UTF8&0&om=0&output=kml");
    Log.d("xxx","URL="+urlString.toString());

    //System.out.println(urlString);
    // get the kml (XML) doc. And parse it to get the coordinates(direction route).
    Document doc = null;
    HttpURLConnection urlConnection= null;
    URL url = null;
    try
    {
      url = new URL(urlString.toString());
      urlConnection=(HttpURLConnection)url.openConnection();
      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(true);
      urlConnection.setDoInput(true);
      urlConnection.connect();

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      doc = db.parse(urlConnection.getInputStream());

      if(doc.getElementsByTagName("GeometryCollection").getLength()>0)
      {
        //String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
        String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ;
        Log.d("xxx","path="+ path);
        String [] pairs = path.split(" ");
        String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height
        // src
        GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
        //mMapView01.getOverlays().add(overlayitem);
        GeoPoint gp1;
        GeoPoint gp2 = startGP;
        for(int i=1;i<pairs.length;i++) // the last one would be crash
        {
          lngLat = pairs[i].split(",");
          gp1 = gp2;
          // watch out! For GeoPoint, first:latitude, second:longitude
          gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
          mMapView01.getOverlays().add(new MapRouteOverlay(gp1,gp2,2,color));
          Log.d("xxx","pair:" + pairs[i]);
        }
        //mMapView01.getOverlays().add(new MapRouteOverlay(dest,dest, 3)); // use the default color
      }
    }
    catch (MalformedURLException e)
    {
      e.printStackTrace();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    catch (ParserConfigurationException e)
    {
      e.printStackTrace();
    }
    catch (SAXException e)
    {
      e.printStackTrace();
    }


  }
  @Override
    protected boolean isRouteDisplayed() {
      // TODO Auto-generated method stub
      return false;
    }
}

MapRouteOverlay.java:

public class MapRouteOverlay extends Overlay {

  private GeoPoint gp1;
  private GeoPoint gp2;

  private int mode=0;
  private int defaultColor;




  public MapRouteOverlay(GeoPoint gp1,GeoPoint gp2,int mode) // GeoPoint is a int. (6E)
  {
    this.gp1 = gp1;
    this.gp2 = gp2;
    this.mode = mode;
    defaultColor = 999; // no defaultColor

  }

  public MapRouteOverlay(GeoPoint gp1,GeoPoint gp2,int mode, int defaultColor)
  {
    this.gp1 = gp1;
    this.gp2 = gp2;
    this.mode = mode;
    this.defaultColor = defaultColor;
  }

  public int getMode()
  {
    return mode;
  }

  public boolean draw
    (Canvas canvas, MapView mapView, boolean shadow, long when)
    {
      Projection projection = mapView.getProjection();
      if (shadow == false)
      {

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Point point = new Point();
        projection.toPixels(gp1, point);

        if(mode==2)
        {
          if(defaultColor==999)
            paint.setColor(Color.RED);
          else
            paint.setColor(defaultColor);
          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);
        }

      }
      return super.draw(canvas, mapView, shadow, when);
    }
}

MapOverlay.java:

public class MapOverlay extends ItemizedOverlay {
  private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

  public MapOverlay(Drawable _defaultMarker) {

    super(boundCenterBottom(_defaultMarker));
  }

  @Override
  protected OverlayItem createItem(int i) {
    return mOverlays.get(i);
  } 
  public void addOverlay(OverlayItem overlay) {
      mOverlays.add(overlay);
      populate();
  }

  @Override
  public int size() {
    return mOverlays.size();
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文