【问题标题】:Google Map-Android Centering and Zooming?Google Map-Android 居中和缩放?
【发布时间】:2015-07-10 17:44:04
【问题描述】:

我正在尝试以一定的缩放比例将地图聚焦在标记上。我查看了 stackoverflow 中的几篇文章,但看不出出现以下错误的任何原因:

java.lang.IllegalStateException: 使用错误 newLatLngBounds(LatLngBounds, int): 地图大小不能为0

很可能,地图视图的布局尚未发生。要么等到布局发生,要么使用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));
}

我调试了代码,它抛出了之前的异常就行了

mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));

这里有什么建议吗?谢谢...

【问题讨论】:

  • @HarshDattani。这没有回答或解决我的问题..我做的一切都是正确的..但仍然得到这个异常..
  • 我认为问题可能与仅将一个位置传递给 LatLngBounds.builder 有关,我认为您至少需要两个位置才能提供大于 0 的大小。

标签: java android google-maps


【解决方案1】:

要仅使用地址中的一个位置,并在该标记上设置缩放,您可以使用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));
    }

调用方法:

try {
    setUpMap("100 Market Street San Francisco CA");
} catch (IOException e) {
    e.printStackTrace();
}

结果:

请注意,通常在使用 LatLngBounds 时,您最初会提供两个点,即边界的东北角和西南角。

一旦设置了这两个边界,您就可以使用include() 方法对expand the bounds if necessary

查看文档:

https://developer.android.com/reference/com/google/android/gms/maps/model/LatLngBounds.html

https://developers.google.com/maps/documentation/android/views

【讨论】:

  • 感谢@DanielNugent 的回答,似乎每次我启动模拟器时它都会崩溃(不是应用程序)。我尝试启动它 3 次,但仍然崩溃.. 可能在后台发生了一些事情。知道为什么
  • 是的,我开启了调试模式,android studio还是抓不到我的手机!!
  • 我有 oneplusone,是的,我之前试过,它成功了.. Android KitKat
  • 我会的,让你知道.. 非常感谢
猜你喜欢
  • 2019-01-20
  • 1970-01-01
  • 2014-10-01
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
相关资源
最近更新 更多