Java 谷歌地图Android居中和缩放?
我试着以一定的缩放比例将地图聚焦在标记上。我查看了stackoverflow中的几篇文章,我看不出出现以下错误的原因: java.lang.IllegalStateException:使用时出错 newLatLngBounds(LatLngBounds,int):映射大小不能为0 很可能地图视图尚未出现布局。等待布局完成,或者使用Java 谷歌地图Android居中和缩放?,java,android,google-maps,Java,Android,Google Maps,我试着以一定的缩放比例将地图聚焦在标记上。我查看了stackoverflow中的几篇文章,我看不出出现以下错误的原因: java.lang.IllegalStateException:使用时出错 newLatLngBounds(LatLngBounds,int):映射大小不能为0 很可能地图视图尚未出现布局。等待布局完成,或者使用newLatLngBounds(LatLngBounds,int,int,int)来指定地图的尺寸 我使用的代码是: private void setUpMap(Str
newLatLngBounds(LatLngBounds,int,int,int)
来指定地图的尺寸
我使用的代码是:
private void setUpMap(String address) throws IOException {
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(address, 1);
Address add = list.get(0);
double Longitude = add.getLongitude();
double Latitude= add.getLatitude();
Marker m = mMap.addMarker(new MarkerOptions().position(new LatLng(Latitude, Longitude)).title(address));
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(m.getPosition());
LatLngBounds bounds = builder.build();
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));
}
有什么建议吗?谢谢…为了只使用地址中的一个位置,并在该标记上设置缩放,您可以使用
CameraPosition
而不是LatLngBounds
我刚刚测试了这个,它可以工作:
private void setUpMap(String address) throws IOException {
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(address, 1);
Address add = list.get(0);
double Longitude = add.getLongitude();
double Latitude= add.getLatitude();
Marker m = mMap.addMarker(new MarkerOptions().position(new LatLng(Latitude, Longitude)).title(address));
//not needed:
//LatLngBounds.Builder builder = new LatLngBounds.Builder();
//builder.include(m.getPosition());
//LatLngBounds bounds = builder.build();
//mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));
//use CameraPosition instead:
LatLng latLng = new LatLng(Latitude, Longitude);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng).zoom(14).build();
//use animateCamera instead of moveCamera:
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
结果:
请注意,通常在使用LatLngBounds
时,最初会提供边界东北角和西南角的两个点
一旦设置了这两个界限,您就可以使用include()
方法来设置
见文件:
可能重复@HarshDattani。这并没有回答或解决我的问题。。我把一切都做对了。。但是仍然有这个例外。我认为问题可能与只将一个位置传递给LatLngBounds.builder有关,我认为至少需要两个位置才能提供大于0的大小。谢谢@DanielNugent的回答,似乎每次启动模拟器时它都会崩溃(而不是应用程序)。我试着启动了3次,但还是失败了。。也许背景中发生了什么。知道为什么吗?我启用了调试模式,android studio仍然无法捕获我的手机!!我有一个Plusone,是的,我以前试过,效果很好。。Android KitKatI会的,并让你知道。。谢谢你给我们一朵荷花。
private void setUpMap(String address) throws IOException {
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(address, 1);
Address add = list.get(0);
double Longitude = add.getLongitude();
double Latitude= add.getLatitude();
Marker m = mMap.addMarker(new MarkerOptions().position(new LatLng(Latitude, Longitude)).title(address));
//not needed:
//LatLngBounds.Builder builder = new LatLngBounds.Builder();
//builder.include(m.getPosition());
//LatLngBounds bounds = builder.build();
//mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));
//use CameraPosition instead:
LatLng latLng = new LatLng(Latitude, Longitude);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLng).zoom(14).build();
//use animateCamera instead of moveCamera:
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
try {
setUpMap("100 Market Street San Francisco CA");
} catch (IOException e) {
e.printStackTrace();
}