2012-02-14 3 views
1

내지도보기에 마커가 여러 개 있습니다. 마커를 클릭하면 현재 위치에서 마커까지 경로가 표시됩니다.안드로이드에서 Google지도에 표시된 경로를 제거하는 방법?

하지만 다음 마커를 클릭하면 경로가 표시되지만 첫 번째 경로는 제거되지 않습니다.

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이다 : DrawPath 방법이다

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

코드로 새 경로를 그릴 때 첫 번째 경로를 제거하는 방법은 무엇입니까?

+0

+1 나는 같은 문제가 있습니다. 그것을 해결합니까? 친절하게 나를 제안 –

답변

0

나는 어떤 종류의 목록에서 루프 (MapRouteOverlay)를 만들고있는 모든 오버레이를 유지해야하고 다음 오버레이를 클릭하면지도에서 모든 것을 제거해야한다고 생각합니다. .........

+0

코드 plz 제안을 통해 할 수있는 방법 .. 내가 안드로이드에 대한 새로운 오전 .. –

0

난 당신의 코드에서 보는 것처럼

mMapView01.getOverlays().add(new MapRouteOverlay(gp1,gp2,2,color)); 

으로지도에 경로를 추가하지만, 당신은 넣어 new 키워드마다 A를 이전 경로 를 제거 해달라고 지도 오버레이로 새 경로 경로를 사용하여 멤버 변수를 만든 다음 매번 새로운 루를 추가하기 전에 해당 경로를 제거하십시오 테

+0

당신이 할 수있는 말을 할 수있는이 .. 내가 안드로이드에 새로운 .. Logged –

관련 문제