Android 安卓谷歌地图移动摄像头不';行不通
我已经看了所有我能找到的关于这个的东西,还没有找到解决办法。我试着做一些非常简单的事情:Android 安卓谷歌地图移动摄像头不';行不通,android,google-maps,Android,Google Maps,我已经看了所有我能找到的关于这个的东西,还没有找到解决办法。我试着做一些非常简单的事情: protected void moveCamera(final LatLng position, final float zoom) { getMap(new OnMapReadyCallback() { @Override public void onMapReady(final GoogleMap googleMap) { logger.
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
,尝试了分别移动和缩放,尝试了使用处理程序延迟发布。有时候这很好,但是如果我把片段和地图放在一起,然后回来,它就不起作用了。地图重置为纬度0,0和标准地图类型(它被设置为混合),我无法让它做任何事情。我知道这个方法是通过记录语句来调用的。我需要做一些生命周期的事情来确保地图可以恢复吗
我真的不想切换到Mapbox或其他东西,因为修复一个小错误需要大量的工作,但我需要它才能正常工作。我使用了以下方法,效果很好:
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
GoogleMap-mGoogleMap;
LatLngBounds bounds=builder.build();
int width=getResources().getDisplayMetrics().widthPixels;
int height=getResources().getDisplayMetrics().heightPixels;
高度=数学圆(0.45f*高度);
整数填充=(整数)(宽度*0.10);
CameraUpdate CameraUpdate=CameraUpdateFactory.newLatLngBounds(边界、宽度、高度、填充);
//将此添加到要移动地图摄影机的位置
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng)(对于我来说,在Places Autocomplete api中OnPlacesSelected回调中的移动摄像头不起作用
我将moveCamera移动到runOnUiThread,现在工作正常。请尝试此代码解决此摄像头问题。
您可以更改缩放级别、半径
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
height = Math.round(0.45f*height);
int padding = (int) (width * 0.10);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLngBounds bounds = builder.include(new LatLng(INSERT_LAT,
INSERT_LNG)).build();
CameraUpdate cameraUpdate =
CameraUpdateFactory.newLatLngBounds(bounds,width,height,padding);
googleMap.moveCamera(cameraUpdate);
googleMap.animateCamera(CameraUpdateFactory.zoomTo(14), 1000, null);
为什么要创建一个CameraUpdate
,然后不使用它呢?如果我只使用moveCamera
和animateCamera
部分,它什么都不做。LatLngBounds bounds bounds=LatLngBounds.builder().build()
(我假设这就是你要做的,因为builder
没有在你的代码片段中定义)产生此错误:java.lang.IllegalStateException:没有包含点
原因是您试图从后台线程执行此操作,并且所有映射操作都必须在主/UI线程上。