2013-03-21 2 views
2

Google지도 API 2를 사용 중이고 여러 마커가 표시됩니다. 내 문제는 내 카메라를 모든 마커에 배치하는 것입니다. 내가보기에 모든 마커를 볼 방법 아래 사용했다여러 마커에서 Google지도 카메라 위치

myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MyLocation, 22)); 

답변

9

로 단일 마커를 수행했다. 그것은 당신의 arraylist에있는 모든 위도 - 긴 위치를 포함하여 Mapview 바운드 것입니다.

// Pan to see all markers in view. 
// Cannot zoom to bounds until the map has a size. 
    final View mapView = getSupportFragmentManager().findFragmentById(R.id.map).getView(); 
    if (mapView.getViewTreeObserver().isAlive()) { 
     mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
      @SuppressLint("NewApi") 
      @Override 
      public void onGlobalLayout() { 
       LatLngBounds.Builder bld = new LatLngBounds.Builder(); 
    for (int i = 0; i < YOUR_ARRAYLIST.size(); i++) {   
      LatLng ll = new LatLng(YOUR_ARRAYLIST.get(i).getPos().getLat(), YOUR_ARRAYLIST.get(i).getPos().getLon()); 
      bld.include(ll);    
    } 
    LatLngBounds bounds = bld.build();   
    mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 70)); 
       mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 

      } 
     }); 
    } 

희망 하시겠습니까?

+0

매력적인 작품. 감사합니다 – Andy

+0

Working like charm! – TharakaNirmana

+0

removeGlobalOnLayoutListener는 더 이상 사용되지 않으므로 대신 removeOnGlobalLayoutListener를 사용하십시오. –

관련 문제