tags:

views:

343

answers:

4

Hi,

I'd like to know if there is already a way to know from a given set of markers, the zoom I should apply to the map or do I have to do it my self? (this depends on the resolution so i expected to find it in MapView because it knows its boundaries...)

Regards, Zied Hamdi

A: 

Hi again,

I've attempted to do a method my self, it doesn't work perfectly but it seems sufficient (maybe I should round the quotient up to have the real value):

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

Best regards, Zied Hamdi

A: 

Hi,

As guessed when I was writing the answer above: when the quotient is eg. 9, it means you need more than 4 iterations to see it: so just correst both lines:

while ((qutient / 2) > 0.5) {

Best Regards, Zied Hamdi

A: 
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 ));
Mike
I've tried it but it doesn't seem to work at all the zoom is huge.
Whoops, you're right that version was broken. I've edited the answer to use fewer temporary objects and also work :)
Mike
A: 

Hi Mike,

I like your code that is a lot shorter ;-), maybe I should only avoid creating new instances in the for loop to have the min and max points...

Best Regards, Zied Hamdi