2017-07-19 88 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