我正在尝试为Android中的 map 设置缩放级别,以使其包括列表中的所有点。我正在使用以下代码。

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

有时这可行。但是有时某些点未显示,因此我需要缩小以查看这些点。有什么办法可以解决这个问题?

最佳答案

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

10-07 19:13
查看更多