Android的地图V2放大,显示所有标记 [英] Android map v2 zoom to show all the markers

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

问题描述

我有10个标志中的 GoogleMap的。我想放大尽可能并保持所有标记的​​看法?在早期版本可从 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);

最后,移动地图:

Finally move the map:

googleMap.moveCamera(cu);

或者,如果你想要一个动画:

Or if you want an animation:

googleMap.animateCamera(cu);

这就是:)

澄清1

几乎所有的运动方法需要地图对象已通过布局的过程。您可以等待此使用 addOnGlobalLayoutListener 结构发生。详细信息可以在评论这个回答和其余的答案被发现。您还可以找到一个完整的code,使用设定的地图范围 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范围实例会东北属性等于西南,这意味着包括在本界地球面积的部分是完全为零。 (这是必然的,因为单个标记有没有面积。)
  2. 将通过范围 CameraUpdateFactory.newLatLngBounds 你基本上是要求计算这样的缩放级别范围(具有零区)将覆盖整个地图视图。
  3. 您可以实际在一张纸上执行该计算。理论缩放级别即答案为+ infin; (正无穷大)。在实践中,地图对象不支持这个值,所以它被钳位在允许给定的位置更合理的最高水平。
  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.

另一种方式把它:怎么能地图对象知道是什么缩放级别应该是选择一个的单个位置的?也许最佳值应为20(如果重新presents特定地址)。或者,也许11(如果重新presents镇)。或者,也许6(如果它再presents的国家)。 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.

所以,你应该简单地检查标记只有一个位置,如果是的话,使用之一:

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

  • CameraUpdate立方米= CameraUpdateFactory.newLatLng(marker.getPosition()) - 去标记的位置,保持当前的缩放级别的完整
  • CameraUpdate立方米= 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天全站免登陆