2010-08-26 14 views

Respuesta

10

He utilizado este método en un reciente proyecto mío

public void centerOverlays() { 
    int minLat = 81 * MapStoresController.MAP_SCALE; 
    int maxLat = -81 * MapStoresController.MAP_SCALE; 
    int minLon = 181 * MapStoresController.MAP_SCALE; 
    int maxLon = -181 * MapStoresController.MAP_SCALE; 

    for (int i = 0; i < overlayItems.size(); i++) { 
     Store s = overlayItems.getItem(i).getStore(); 
     minLat = (int) ((minLat > (s.getLocation().getLatitude() * MapStoresController.MAP_SCALE)) ? s.getLocation().getLatitude() * MapStoresController.MAP_SCALE : minLat); 
     maxLat = (int) ((maxLat < (s.getLocation().getLatitude() * MapStoresController.MAP_SCALE)) ? s.getLocation().getLatitude() * MapStoresController.MAP_SCALE : maxLat); 
     minLon = (int) ((minLon > (s.getLocation().getLongitude() * MapStoresController.MAP_SCALE)) ? s.getLocation().getLongitude() * MapStoresController.MAP_SCALE : minLon); 
     maxLon = (int) ((maxLon < (s.getLocation().getLongitude() * MapStoresController.MAP_SCALE)) ? .getLocation().getLongitude() * MapStoresController.MAP_SCALE : maxLon); 
    } 

    GeoPoint gp = controller.getUserLocation(); 

    minLat = (minLat > gp.getLatitudeE6()) ? gp.getLatitudeE6() : minLat; 
    maxLat = (maxLat < gp.getLatitudeE6()) ? gp.getLatitudeE6() : maxLat; 
    minLon = (minLon > gp.getLongitudeE6()) ? gp.getLongitudeE6() : minLon; 
    maxLon = (maxLon < gp.getLongitudeE6()) ? gp.getLongitudeE6() : maxLon; 

    mapView.getController().zoomToSpan((maxLat - minLat), (maxLon - minLon)); 
    mapView.getController().animateTo(new GeoPoint((maxLat + minLat)/2, (maxLon + minLon)/2)); 
} 

Básicamente se encuentra a los límites de una caja y establece el objeto de la más cercana que abarca los límites

+0

¿dónde se define MapStoresController? No puedo encontrar su referencia, ¿es parte de la API de Google Map? – ala

+1

Es algo que implementé yo mismo. La propiedad MAP_SCALE se define como: public static final int MAP_SCALE = (int) 1E6; –

+0

La solución de Schwiz 'a continuación es mucho mejor –

26

esto es mucho más fácil solución si está usando un ItemizedOverlay

public void fitOverlays() { 
     mMapView.getController().zoomToSpan(mItemizedOverlay.getLatSpanE6(), mItemizedOverlay.getLonSpanE6()); 
} 
+0

donde llamar a esta función? – Krishnabhadra

+0

siempre que agregue más superposiciones, supongo, o si hay un botón de acercamiento para ajustar. Realmente es una pregunta de diseño que tienes. – schwiz

Cuestiones relacionadas