2017-05-02 2 views
0

문제는 다른 경로를 그릴 때마다지도가 지워지지 않으므로 한 경로가 다른 경로 위에 그려지는 것입니다.Google지도 개체의 명확한 경로 - android

나는 그것을 해결하기 위해 이미 너무 많은 시간을 보냈습니다. 결국 다른 사용자가 제안한 것을 테스트 한 후에는 아무 것도 작동하지 않는 것 같습니다. 이것은 기본적으로 내가 무엇을 시도했다입니다 :

  • 나는 각 폴리 라인에 대한 참조를 유지하고있어

  • 나중에 polyline.remove() 방법 때를 호출지도를 추가 googleMap.clear() 메소드를 호출하고 있습니다 새 경로를 그리기.

나는 정지 객체는지도에 경로를 그리는 데 필요한 정보를 포함하는 버스 정류장을 나타내는, 데이터베이스에서 데이터를 불러 오는하는 웹 서비스는, 그것이 ArrayList<Stop> 반환해야합니다.

직접 구글에서 웹 서비스를 사용하지 않는 응용 프로그램은 각각의 정지 객체는 "폴리 라인"이라는 필드가 앱이 폴리 라인 문자열을 디코딩하고 ArrayList<LatLng>

은 내가 당신에게 내가 쓴 코드의 조각을 보여 드리죠 검색

그것을하기 위해.

public void drawRoute(ArrayList<Stop> stops){ 
    googleMap.clear(); 
    removePolylines(); 
     for(int x = 0; x< stops.size(); x++){ 
      Stop stop = stops.get(x); 
      if(x != stops.size() -1){      
       ArrayList<LatLng> routePoints = DataParser.decodePolyline(stop.getPolyline()); 
       Polyline polyline = googleMap.addPolyline(polylineOptions.addAll(routePoints)); 
       polylines.add(polyline);      
      } 
      addMarker(stop); 
     }    
} 

...

private void removePolylines(){ 
    for(Polyline polyline: polylines){ 
      polyline.remove(); 
    } 
} 

또한 문제가 무엇인지 잘 설명하기 위해 스크린 샷을 첨부. 경로를 처음 그리기 아무 문제가 없습니다

enter image description here

...

enter image description here

그러나 내가 뒤로 버튼을 눌러 다른 경로 ID를 선택하면 ...

enter image description here

빨간색 원으로 볼 수 있듯이 이전 경로는 sti입니다. 이상한 것은 표식이 더 이상 나타나지 않지만 경로는 그렇다는 것입니다.

이 문제를 어떻게 해결할 수 있습니까? 더 좋은 해결책이 있습니까?

답변

0

polyline.remove(); 문 뒤에 polylines.remove(polyline);을 추가해야하는 것처럼 보입니다. 참고로 다음은 비슷한 질문입니다. Remove the last plotted line from Google Map Android

+0

나는 똑같은 생각을하고있었습니다. – danny117

+0

@Andrew Kulpa 방금 해결책을 찾았습니다. 시간 내 주셔서 감사합니다. – Sandoval0992

0

많은 사람들이 투쟁을하고 나면 해결책이 얼마나 쉬운 지 깨달았습니다.

문제는이 라인에서 발생

Polyline polyline = googleMap.addPolyline(polylineOptions.addAll(routePoints)); 

것은이 polylineOptions 객체가 클래스의 상단에 선언하고 생성자에서 initilalized된다는 점이다.따라서 drawRoute(ArrayList<Stop> stops) 메서드가 실행될 때마다 새로 ArrayList<LatLng> routePoints이 추가됩니다.

te 경로가 다른 경로 위에 표시되는 이유입니다. 적어도이 구체적인 경우에는 googleMap.clear()removePolylines() 메서드를 호출 할 필요가 없음을 알았습니다.

리팩토링 코드는 다음과 같습니다

public void drawRoute(ArrayList<Stop> stops){ 
    PolylineOptions polylineOptions = new PolylineOptions(); 
    for(int x = 0; x< stops.size(); x++){ 
     Stop stop = stops.get(x); 
     if(x != stops.size() -1){ 
      ArrayList<LatLng> routePoints = DataParser.decodePolyline(stop.getPolyline()); 
      googleMap.addPolyline(polylineOptions.addAll(routePoints));     
     } 
     addMarker(stop); 
    } 
} 

당신은 차이를 볼? PolylineOptions 개체는 메서드 내에서 로컬로 선언되고 초기화됩니다.

다른 사람들에게 유용하게되기를 바랍니다.