如何根据返回的标记计算缩放级别?

时间:2009-08-05 13:43:34

标签: android

我想知道是否已经有一种方法可以从一组给定的标记中知道,缩放我应该应用于地图还是我必须自己做? (这取决于分辨率,所以我希望在MapView中找到它,因为它知道它的边界。)

4 个答案:

答案 0 :(得分:4)

int minLat = Integer.MAX_VALUE;
int minLong = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int maxLong = Integer.MIN_VALUE;

for( GeoPoint l : points ) {
    minLat  = Math.min( l.getLatitudeE6(), minLat );
    minLong = Math.min( l.getLongitudeE6(), minLong);
    maxLat  = Math.max( l.getLatitudeE6(), maxLat );
    maxLong = Math.max( l.getLongitudeE6(), maxLong );
}

mapView.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));

答案 1 :(得分:0)

我试图自己做一个方法,它不能很好地工作但看起来已经足够了(也许我应该将商数四舍五入以获得真正的价值):

private void adjustZoomToMarkers(ArrayList<GeoLocationFlag> flags) {
    GeoPoint mapCenter = mapView.getMapCenter();
    int lat = mapCenter.getLatitudeE6(), lng = mapCenter.getLongitudeE6();
    int farestLat = 0, farestLng = 0;
    for (GeoLocationFlag geoLocationFlag : flags) {
        Log.d(LOG_TAG, "lat: " + geoLocationFlag.getLat());
        int flagLatDistance = Math.abs(geoLocationFlag.getLat() - lat);
        if (farestLat < flagLatDistance)
            farestLat = flagLatDistance;

        Log.d(LOG_TAG, "lng: " + geoLocationFlag.getLng());
        int flagLngDistance = Math.abs(geoLocationFlag.getLng() - lng);
        if (farestLng < flagLngDistance)
            farestLng = flagLngDistance;
    }
    Log.d(LOG_TAG, "farest: " + farestLat + "," + farestLng);
    Log.d(LOG_TAG, "spans: " + mapView.getLatitudeSpan() + "," + mapView.getLongitudeSpan());

    // compute how many times this screen we are far on lat
    float latQuotient = (float) farestLat / ((float) mapView.getLatitudeSpan() / 2);
    // compute how many times this screen we are far on lng
    float lngQuotient = (float) farestLng / ((float) mapView.getLongitudeSpan() / 2);

    int zoom = 0;
    if (latQuotient > 1 || lngQuotient > 1) {
        // must zoom out
        float qutient = Math.max((int) latQuotient, (int) lngQuotient);
        while ((qutient / 2) > 1) {
            qutient = qutient / 2;
            zoom--;
        }
    } else {
        float qutient = Math.max((int) (1 / (float) latQuotient), (int) (1 / (float) lngQuotient));
        while ((qutient / 2) > 1) {
            qutient = qutient / 2;
            zoom++;
        }

    }
    Log.d(LOG_TAG, "Zoom found " + zoom);

    int zoomLevel = mapView.getZoomLevel();
    mapController.setZoom(zoomLevel + zoom);
}

祝你好运, Zied Hamdi

答案 2 :(得分:0)

当我在写上面的答案时猜到:当商是例如。 9,这意味着您需要超过4次迭代才能看到它:  所以只需纠正两条线:

while ((qutient / 2) > 0.5) {

最诚挚的问候, Zied Hamdi

答案 3 :(得分:0)

我喜欢你的代码更短;-),也许我应该避免在for循环中创建新实例以获得最小和最大点...

最诚挚的问候, Zied Hamdi

相关问题