2015-04-29 54 views
-1

在Google Maps API v2中,方法animateCamera用於定義查看的縮放級別。例如,Google Maps API v2中的動態縮放Android

googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat1, lng1), 12)); 

這裏,縮放級別固定爲12.如何確保這裏固定變焦值,將允許用戶沿線查看源代碼和目標點,無需手動縮放

我知道使用試錯法和一些if-else條件,可以確定這些值。但是,有沒有更復雜的方式來做到這一點?

回答

5

可以使用的LatLngBounds這樣的:

LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
builder.include(startPoint); 
builder.include(endPoint); 
LatLngBounds bound = builder.build(); 
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bound, 25), 1000, null); 

的startPoint和端點LatLng對象。

0

Rajat的答案是正確的,但您需要等待地圖加載,否則您可能會收到錯誤「地圖大小爲0」。

googleMap.setOnMapLoadedCallback(this); 

...

@Override 
public void onMapLoaded() { 
    if (googleMap != null) { 
     LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
     builder.include(startPoint); 
     builder.include(endPoint); 
     LatLngBounds bound = builder.build(); 

     googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bound, 40)); 
    } 
} 
相關問題