【问题标题】:how to remove route displayed on google map in android?如何在android中删除谷歌地图上显示的路线?
【发布时间】:2012-02-14 09:13:56
【问题描述】:

我的地图视图上有多个标记,当我点击标记时,它会显示从当前位置到标记的路线。

但是当我点击下一个标记时,它会显示路线但不会删除第一条路线。 在地图上绘制新路线时如何删除第一条路线

我使用的代码是

在标记上的单击侦听器中,我这样调用 DrawPath 方法:

    GeoPoint srcpoint = new GeoPoint(Source geopoint);

    GeoPoint destpoint = new GeoPoint(Destination geo point);

    DrawPath(srcpoint, destpoint, Color.GRAY, mapView);

而DrawPath方法是:

  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();
        }


      }

上述方法中使用的类 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.GRAY);
      else
        paint.setColor(Color.GRAY);
      Point point2 = new Point();
      projection.toPixels(gp2, point2);
      paint.setStrokeWidth(5);
      paint.setAlpha(120);
    //  canvas.restore();
      canvas.drawLine(point.x, point.y, point2.x,point2.y, paint);
    }

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

代码绘制新路径时如何删除第一条路径?

【问题讨论】:

  • +1 我也有同样的问题。你解决了吗?请建议我

标签: java android routes android-maps


【解决方案1】:

我认为您必须将所有在 for 循环 (MapRouteOverlay) 中制作的叠加层保留在某种列表中,然后当您单击下一个叠加层时,您将不得不从地图中删除所有这些叠加层...... ........

【讨论】:

    【解决方案2】:

    正如我在您的代码中看到的,您使用

    将路线添加到地图中
    mMapView01.getOverlays().add(new MapRouteOverlay(gp1,gp2,2,color));
    

    但你没有删除旧路线 使用 new 关键字,您每次将新路线放入地图叠加层 用你的路由创建一个成员变量,然后在每次添加新路由之前删除它们

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 2012-07-28
      • 2011-08-10
      • 1970-01-01
      • 2011-07-22
      • 2011-07-11
      • 2021-03-07
      • 1970-01-01
      相关资源
      最近更新 更多