java - 如何计算 map 跨度/缩放以在视口(viewport)中包含一组 GeoPoints?

标签 java android algorithm google-maps

我有一组 GeoPoints,想找到包含所有这些 GeoPoints 的纬度/经度中心和跨度(因此它们在 MapView 上都可见)。

在平面 map 上,这会相当简单,找到最高和最低的 x/y 值,通过将绝对距离的一半加到较低的值来找到中心。

但是对于球形世界地图,这是如何做到的,其中 -180/+180 和 +90/-90 的最大/最小值彼此相邻?

这是我正在尝试做的事情:

public void zoomToGeoPoints(GeoPoint... geoPoint) {

    MapController mc = getController();
    if (geoPoint == null) {
        Log.w(TAG, "No geopoints passed, doing nothing!");
        return;
    }

    // Set inverse max/min start values 
    int maxLat = (int) (-90 * 1E6);
    int maxLon = (int) (-180 * 1E6);
    int minLat = (int) (90 * 1E6);
    int minLon = (int) (180 * 1E6);

    // Find the max/min values
    for (GeoPoint gp : geoPoint) {
        maxLat = Math.max(maxLat, gp.getLatitudeE6());
        maxLon = Math.max(maxLon, gp.getLongitudeE6());
        minLat = Math.min(minLat, gp.getLatitudeE6());
        minLon = Math.min(minLon, gp.getLongitudeE6());
    }

    // Find the spans and center point
    double spanLat = Math.abs(maxLat - minLat);
    double spanLon = Math.abs(maxLon - minLon);

    int centerLat = (int) ((minLat + spanLat / 2d));
    int centerLon = (int) ((minLon + spanLon / 2d));

    GeoPoint center = new GeoPoint(centerLat, centerLon);

    // Pan to center
    mc.animateTo(center);           

    // Zoom to include all GeoPoints
    mc.zoomToSpan((int)(spanLat), (int)(spanLon));

最佳答案

未经测试,使用 JavaScript 中的 map API 计算所需的最小边界框:

// points[] is your array of geo points
var boundbox = new map.LatLngBounds();
for (var i=0; i<points.length; i++){ 
  boundbox.extend(points[i]);
}
var center = boundbox.getCenter();

关于java - 如何计算 map 跨度/缩放以在视口(viewport)中包含一组 GeoPoints?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11939409/

相关文章:

java - 从一组点创建多边形

java - 在 catch block 中调用 ReentrantReadWriteLock#lock() 是否错误?

java - 如何使用 SimpleXml 和 Java 动态生成 XML?元素,ElementList 应该动态出现在随机位置

android - Wifi 功能

java - 从我的应用程序运行 shell 实用程序会导致其使用 100% CPU

algorithm - 归并排序的 K 路归并操作

java - : binary,解释、执行、编译这几个概念之间有什么关系?

android - 如何使用 url_launcher 包只打开默认邮件应用程序,而无需撰写

c++ - STL 算法如何识别容器?

algorithm - 计算网格中的连接点