java Android 谷歌地图 API V2 中心标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17613940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:28:26  来源:igfitidea点击:

Android Google maps API V2 center markers

javaandroidgoogle-maps

提问by Somk

Is there a way to make a map zoom in as far as possible but keeping all of the markers on screen at the same time. I have six markers in a non uniform shape. I was thinking of just taking their centre and locating the map to that, but I still then do not know how far programmatically I can zoom in the map.

有没有办法使地图尽可能放大,但同时将所有标记保留在屏幕上。我有六个形状不统一的标记。我想只是把他们的中心定位到那个位置,但我仍然不知道我可以通过编程将地图放大多远。

These markers change location every time the map is created. Ideally I would have a script that zooms the map in so all are included in the view.

每次创建地图时,这些标记都会更改位置。理想情况下,我会有一个放大地图的脚本,以便所有内容都包含在视图中。

So you understand. I am creating the markers one at a time from json with the following line in a loop.

这样你就明白了。我正在从 json 中一次创建一个标记,并在循环中使用以下行。

mMap.addMarker(new MarkerOptions().position(new LatLng(lat,lng)).title(c.getString("title")));

回答by Jai Kumar

You should use the CameraUpdate class to do (probably) all programmatic map movements.

您应该使用 CameraUpdate 类来执行(可能)所有的程序化地图移动。

To do this, first calculate the bounds of all the markers like so:

为此,首先计算所有标记的边界,如下所示:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for each (Marker m : markers) {
    builder.include(m.getPosition());
}

LatLngBounds bounds = builder.build();

Then obtain a movement description object by using the factory: CameraUpdateFactory:

然后使用工厂获取运动描述对象:CameraUpdateFactory:

int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);

Finally move the map:

最后移动地图:

googleMap.moveCamera(cu);

Or if you want an animation:

或者,如果您想要动画:

googleMap.animateCamera(cu);

*Got from another post: Android map v2 zoom to show all the markers

*来自另一篇文章:Android 地图 v2 缩放以显示所有标记