2015-01-05 1 views
0

사용자가 찍은 경로의 모든 지점을 볼 수 있도록 화면의지도를 업데이트해야하는 문제에 직면하고 있습니다.알 수없는지도 업데이트가 완료되었는지 확인하는 방법은 무엇입니까?

아래 코드에서 맵 업데이트를 요청한 횟수를 계산하지만 때로는 요청 수가 콜백 수와 일치하지 않는 것으로 나타났습니다. 그래서 'mapLoaded'가 0이되기를 기다리는 것은 좋은 생각이 아닙니다.

따라서 제한 시간을 10 초로 늘 렸지만 이는 임의적이며 때로는 충분하지 않습니다. 그럼 모든지도 업데이트가 완료되었는지 어떻게 알 수 있습니까?

private void adjustMapCompleteSO(LatLng from, LatLng to){//3.3.17 show all points for screenshot 
    double x1=(from.latitude+to.latitude)/2; 
    double x2=(from.longitude+to.longitude)/2; 
    LatLng del=new LatLng(x1,x2); 
    map.moveCamera(CameraUpdateFactory.newLatLng(del)); 

    mapLoaded=0; 

    for(Polyline pol : allcrumbs){ 
     List<LatLng> points = pol.getPoints(); 
     for (LatLng point : points){ 

      LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
      builder.include(point); 

      LatLngBounds bounds = builder.build(); 
      int padding = 40; // offset from edges of the map in pixels 

      CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding); 

      mapLoaded++; 
      map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() { 
       public void onMapLoaded() { 
        mapLoaded--; 
       } 
      }); 

      map.moveCamera(cu); 
     } 
    } 
    Date started = new Date(); 
    while (mapLoaded !=0 && new Date().getTime() - started.getTime() < 10000){ 
     try {//wait until map has loaded, but max 10 seconds 
      Thread.sleep(500);//wait half a second before tyring again 
     } catch (InterruptedException e) {} 
    } 
} 

답변

1

지도에 모든 폴리선을 표시하십시오.

긴 경계 빌더를 위도로 보내기 폴리 라인의 모든 지점을 통해 빌더에게

 LatLngBounds.Builder builder = new LatLngBounds.Builder(); 

반복 처리를 만듭니다.

for(Polyline pol : allcrumbs){ 
    List<LatLng> points = pol.getPoints(); 
    for (LatLng point : points){ 
     // dude never initialize variables in a loop again 
     // its automatic fail for speed of execution. 
     // String never = "Do this in a loop"; 
     // int padding = 40; // offset from edges of the map in pixels 
     builder.include(point); 
    } 
} 

지금

LatLngBounds bounds = builder.build(); 
int padding = 40; // offset from edges of the map in pixels 
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding); 
map.moveCamera(cu); 

당신은 그래서 maploaded 콜백에서 뭘 IDK 카메라를 움직이 아니 위의 코드이다.

팁 : 폴리 라인을 만들 때 latlngbounds.builder를 채우십시오. 폴리 라인로드가 완료되면 카메라를 이동하십시오.

LatLngBounds bounds = builder.build(); 
int padding = 40; // offset from edges of the map in pixels 
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding); 
map.moveCamera(cu); 

참고 : 경로를 따라 카메라를 이동하는 코드와 유사 할 것입니다하지만 카메라가 각 포인트에 대한 완료 할 때 일반적으로 만 카메라를 업데이트 할 것입니다.

관련 문제