43

我正在尝试在 android 中设置地图的缩放级别,使其包含我列表中的所有点。我正在使用以下代码。

int minLatitude = Integer.MAX_VALUE;
int maxLatitude = Integer.MIN_VALUE;
int minLongitude = Integer.MAX_VALUE;
int maxLongitude = Integer.MIN_VALUE;

// Find the boundaries of the item set
// item contains a list of GeoPoints
for (GeoPoint item : items) { 
    int lat = item.getLatitudeE6();
    int lon = item.getLongitudeE6();

    maxLatitude = Math.max(lat, maxLatitude);
    minLatitude = Math.min(lat, minLatitude);
    maxLongitude = Math.max(lon, maxLongitude);
    minLongitude = Math.min(lon, minLongitude);
}
objMapController.zoomToSpan(
    Math.abs(maxLatitude - minLatitude), 
    Math.abs(maxLongitude - minLongitude));

这有时有效。但是,有时某些点未显示,我需要然后缩小以查看这些点。有没有办法解决这个问题?

4

3 回答 3

69

Android Map API v2 的另一种方法:

private void fixZoom() {
    List<LatLng> points = route.getPoints(); // route is instance of PolylineOptions 

    LatLngBounds.Builder bc = new LatLngBounds.Builder();

    for (LatLng item : points) {
        bc.include(item);
    }

    map.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), 50));
}
于 2012-12-26T02:05:24.717 回答
31

我自己找到了答案,缩放级别是正确的。我需要添加以下代码以在屏幕上显示所有点。

objMapController.animateTo(new GeoPoint( 
    (maxLatitude + minLatitude)/2, 
    (maxLongitude + minLongitude)/2 )); 

中心点没有正确对齐,给我带来了问题。这行得通。

于 2011-02-25T10:44:50.140 回答
0

部分问题可能是 MIN_VALUE 仍然是正数,但纬度和经度可以是负数。尝试使用 NEGATIVE_INFINITY 而不是 MIN_VALUE。

于 2011-10-12T18:29:49.060 回答