在 Android 上获取给定边界的地图缩放级别,就像在 JS Google Maps API 上一样:map.getBoundsZoomLevel(bounds)

发布于 2024-10-17 06:24:43 字数 301 浏览 4 评论 0原文

我有一个簇标记,它定义了包含一组标记的边界矩形。该簇具有中心(纬度、经度)、标记计数以及用于计算边界的纬度和经度跨度。

Google Maps API (JS) 有一个名为“getBoundsZoomLevel(bounds)”的函数,但我在 Android 版 Google Maps SDK 中找不到等效的方法。如何估计给定范围的缩放级别(尤其是在具有不同分辨率/密度的不同设备上)?

我计划用户可以触摸集群标记,地图视图将以该集群的中心和边界为中心。

有人给我提供工作代码片段或一些建议吗?

提前致谢 问候托尔斯滕。

I have a cluster marker that defines a bounding rectangle containing a group of markers. The cluster has a center (lat,lon), a marker count and a latitude and longitude span to calculate the bounds.

The Google Maps API (JS) has a function called "getBoundsZoomLevel(bounds)" but I cannot find an equivalent method in the Google Maps SDK for Android. How can I estimate the zoom level for given bounds (especially on different devices with different resolutions/densitys)?

I've planned that an user can touch a cluster marker and the map view will be centered to the center and the bounds of that cluster.

Has anybody a working code snippet or some suggestions for me?

Thanks in advance
Regards Thorsten.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

习ぎ惯性依靠 2024-10-24 06:24:43

谢谢您的回答索尔维克。与此同时,我找到了一个稍微修改过的解决方案,并且它有效。该方法计算一组地理点的边界,计算这些点的跨度,最后使用 ZoomToSpan 和 animateTo 来放大给定区域并将其居中:

 public static void zoomInBounds(final MapView view, final GeoPoint.. bounds) {

    int minLat = Integer.MAX_VALUE;
    int minLong = Integer.MAX_VALUE;
    int maxLat = Integer.MIN_VALUE;
    int maxLong = Integer.MIN_VALUE;

    for (GeoPoint point : bounds) {
        minLat = Math.min(point.getLatitudeE6(), minLat);
        minLong = Math.min(point.getLongitudeE6(), minLong);
        maxLat = Math.max(point.getLatitudeE6(), maxLat);
        maxLong = Math.max(point.getLongitudeE6(), maxLong);
    }

    final MapController controller = view.getController();
    controller.zoomToSpan(
                       Math.abs(minLat - maxLat), Math.abs(minLong - maxLong));
    controller.animateTo(new GeoPoint((maxLat + minLat) / 2,
        (maxLong + minLong) / 2));
}

Thank you for your answer Solvek. Meanwhile I found a solution that I've adapted slightly and it works. The method computes the bounds of a set of geo points, computes the span of these points and finally uses zoomToSpan and animateTo for zooming into and centering the given area:

 public static void zoomInBounds(final MapView view, final GeoPoint.. bounds) {

    int minLat = Integer.MAX_VALUE;
    int minLong = Integer.MAX_VALUE;
    int maxLat = Integer.MIN_VALUE;
    int maxLong = Integer.MIN_VALUE;

    for (GeoPoint point : bounds) {
        minLat = Math.min(point.getLatitudeE6(), minLat);
        minLong = Math.min(point.getLongitudeE6(), minLong);
        maxLat = Math.max(point.getLatitudeE6(), maxLat);
        maxLong = Math.max(point.getLongitudeE6(), maxLong);
    }

    final MapController controller = view.getController();
    controller.zoomToSpan(
                       Math.abs(minLat - maxLat), Math.abs(minLong - maxLong));
    controller.animateTo(new GeoPoint((maxLat + minLat) / 2,
        (maxLong + minLong) / 2));
}
若言繁花未落 2024-10-24 06:24:43

现在您可以使用 .newLatLngBounds() 方法缩放到边界:

map.animateCamera(CameraUpdateFactory.newLatLngBounds(place.getViewport(), 10)); // 10 is padding

它将移动并缩放到给定的边界,因此它们都在屏幕上可见(例如,城市或国家的不同缩放)。它与位置搜索完美配合。

Now you could zoom to bounds with .newLatLngBounds() method:

map.animateCamera(CameraUpdateFactory.newLatLngBounds(place.getViewport(), 10)); // 10 is padding

It will move and zoom to given bounds, so they all visible on a screen (e.g. different zoom for city or country). It works perfectly with location search.

清醇 2024-10-24 06:24:43

我没有尝试过这个,但也许它对你有用。假设你有一个矩形 (x1,y1)-(x2,y2)

    MapController c = mapView.getController();
    c.setCenter(new GeoPoint((x1+x2)/2,(y1+y2)/2));
    c.zoomToSpan(x2, y2);

I didn't tried this but maybe it will work for you. Imagine you have a rectangle (x1,y1)-(x2,y2)

    MapController c = mapView.getController();
    c.setCenter(new GeoPoint((x1+x2)/2,(y1+y2)/2));
    c.zoomToSpan(x2, y2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文