【发布时间】: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));
【问题讨论】:
-
与Google MAP API v3: Center & Zoom on displayed markers 的问题非常相似,而google map zoom to fit 的信息量则更少。
标签: java android algorithm google-maps