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

Tus*_*kar 42 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));
Run Code Online (Sandbox Code Playgroud)

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

iut*_*nvg 68

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));
}
Run Code Online (Sandbox Code Playgroud)


Tus*_*kar 31

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

objMapController.animateTo(new GeoPoint( 
    (maxLatitude + minLatitude)/2, 
    (maxLongitude + minLongitude)/2 )); 
Run Code Online (Sandbox Code Playgroud)

中心点不是为了给我带来问题.这有效.

  • objMapController是什么对象?如何创建此引用可以发布吗? (2认同)