简体   繁体   English

Android谷歌地图API V2中心标记

[英]Android Google maps API V2 center markers

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")));

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缩放以显示所有标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM