Android:在Google地图中设置缩放级别以包含所有标记点

时间:2011-02-25 07:31:35

标签: android google-maps android-maps

我正在尝试为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));

这有时会起作用。但是有时候某些点没有显示,我需要缩小以查看这些点。有什么方法可以解决这个问题吗?

3 个答案:

答案 0 :(得分:67)

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));
}

答案 1 :(得分:31)

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

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

中心点并非对齐而是为我创造问题。这很有效。

答案 2 :(得分:0)

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