【问题标题】:How to calculate the map span/zoom to include a set of GeoPoints in the viewport?如何计算地图跨度/缩放以在视口中包含一组地理点?
【发布时间】:2012-08-09 23:36:34
【问题描述】:

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

在平面地图上这很简单,找到最高和最低的 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));

【问题讨论】:

标签: java android algorithm google-maps


【解决方案1】:

未经测试,使用 JavaScript 中的 Maps 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();

【讨论】:

  • 我认为 Maps API V2 中的 LatLngBounds() 助手可以解决问题。由于 API v2 现在是默认设置,我将其标记为答案。
猜你喜欢
  • 2011-12-09
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多