2017-07-19 40 views
0

我已經看過我能找到的所有東西,還沒有找到解決方案。我試圖做一些非常簡單的事情:安卓谷歌地圖移動相機不起作用

protected void moveCamera(final LatLng position, final float zoom) { 
    getMap(new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(final GoogleMap googleMap) { 

      logger.debug("Moving map to " + position); 
      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(position) 
        .zoom(zoom) 
        .build(); 
      googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

     } 
    }); 
} 

沒有任何反應。沒有錯誤,沒有相機移動。相反,我也試過moveCamera。我嘗試了不同類型的電話CameraUpdateFactory,我嘗試了移動和縮放分開,我試圖推遲張貼與Handler。有時候,這可以很好地工作,但是如果我把碎片放在地圖上並返回,那麼它就不起作用。地圖重置爲緯度0,0和標準地圖類型(它被設置爲混合),我無法讓它做任何事情。我知道這個方法正在被日誌語句調用。是否有某種生命週期的事情我需要做,以確保地圖可以恢復?

我真的不想切換到Mapbox或其他東西,因爲這將是很多工作來解決一個小錯誤,但我需要這個正常工作。

回答

0

我用下面的,它工作正常:

GoogleMap mGoogleMap; 

LatLngBounds bounds = builder.build(); 
int width = getResources().getDisplayMetrics().widthPixels; 
int height = getResources().getDisplayMetrics().heightPixels; 
height = Math.round(0.45f*height); 
int padding = (int) (width * 0.10); 


CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds,width,height,padding); 

// Add this where you want to move map camera 
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng("<pass_LatLng_here")); 
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(14)); //set the zoom level here 
+0

爲什麼你創建一個'CameraUpdate',然後不使用它呢? – nasch

+0

如果我只是使用'moveCamera'和'animateCamera'部件,它什麼都不做。 'LatLngBounds bounds = LatLngBounds.builder()。build()'(我假設你正在做什麼,因爲'builder'沒有在你的代碼片段中定義)會產生這樣的錯誤:'java.lang.IllegalStateException:no included points ' – nasch