Android 地图 v2 缩放以显示所有标记 [英] Android map v2 zoom to show all the markers

查看:31
本文介绍了Android 地图 v2 缩放以显示所有标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 GoogleMap 中有 10 个标记.我想尽可能放大并保留所有标记?在早期版本中,这可以通过 zoomToSpan() 实现,但在 v2 中我不知道如何做到这一点.此外,我知道需要可见的圆的半径.

I have 10 markers in the GoogleMap. I want to zoom in as much as possible and keep all markers in view? In the earlier version this can be achieved from zoomToSpan() but in v2 I have no idea how about doing that. Further, I know the radius of the circle that needs to be visible.

推荐答案

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

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

为此,首先像这样计算所有标记的边界:

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

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markers) {
    builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();

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

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

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

最后移动地图:

googleMap.moveCamera(cu);

或者如果您想要动画:

googleMap.animateCamera(cu);

就是这样:)

说明 1

几乎所有的移动方法都需要 Map 对象通过布局过程.您可以使用 addOnGlobalLayoutListener 构造等待这种情况发生.详细信息可以在对此答案和其余答案的评论中找到.您还可以在此处找到使用addOnGlobalLayoutListener设置地图范围的完整代码.

Almost all movement methods require the Map object to have passed the layout process. You can wait for this to happen using the addOnGlobalLayoutListener construct. Details can be found in comments to this answer and remaining answers. You can also find a complete code for setting map extent using addOnGlobalLayoutListener here.

说明 2

一个评论指出,仅对一个标记使用此方法会导致地图缩放设置为奇异"缩放级别(我认为这是给定位置可用的最大缩放级别).我认为这是意料之中的,因为:

One comment notes that using this method for only one marker results in map zoom set to a "bizarre" zoom level (which I believe to be maximum zoom level available for given location). I think this is expected because:

  1. LatLngBounds bounds 实例将具有等于 southwestnortheast 属性,这意味着该 bounds 正好为零.(这是合乎逻辑的,因为单个标记没有区域.)
  2. 通过将 bounds 传递给 CameraUpdateFactory.newLatLngBounds,您实际上要求计算bounds(具有零区域)将覆盖的缩放级别整个地图视图.
  3. 您实际上可以在一张纸上执行此计算.作为答案的理论缩放级别是 +∞(正无穷大).在实践中,Map 对象不支持此值,因此它被限制为给定位置允许的更合理的最大级别.
  1. The LatLngBounds bounds instance will have northeast property equal to southwest, meaning that the portion of area of the earth covered by this bounds is exactly zero. (This is logical since a single marker has no area.)
  2. By passing bounds to CameraUpdateFactory.newLatLngBounds you essentially request a calculation of such a zoom level that bounds (having zero area) will cover the whole map view.
  3. You can actually perform this calculation on a piece of paper. The theoretical zoom level that is the answer is +∞ (positive infinity). In practice the Map object doesn't support this value so it is clamped to a more reasonable maximum level allowed for given location.

另一种说法:Map 对象如何知道它应该为单个位置选择什么缩放级别?也许最佳值应该是 20(如果它代表一个特定的地址).或者也许是 11(如果它代表一个城镇).或者可能是 6(如果它代表一个国家).API 不是那么聪明,决定权在你.

Another way to put it: how can Map object know what zoom level should it choose for a single location? Maybe the optimal value should be 20 (if it represents a specific address). Or maybe 11 (if it represents a town). Or maybe 6 (if it represents a country). API isn't that smart and the decision is up to you.

因此,您应该简单地检查 markers 是否只有一个位置,如果有,请使用以下之一:

So, you should simply check if markers has only one location and if so, use one of:

  • CameraUpdate cu = CameraUpdateFactory.newLatLng(marker.getPosition()) - 转到标记位置,保持当前缩放级别不变.
  • CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 12F) - 转到标记位置,将缩放级别设置为任意选择的值 12.
  • CameraUpdate cu = CameraUpdateFactory.newLatLng(marker.getPosition()) - go to marker position, leave current zoom level intact.
  • CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 12F) - go to marker position, set zoom level to arbitrarily chosen value 12.

这篇关于Android 地图 v2 缩放以显示所有标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆