Monday, April 16, 2012

Android: Setting Zoom Level in Google Maps to include all Marker points


I am trying to set zoom level for Maps in android such that it includes all the points in my list. I am using following code.




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
for (GeoPoint item : items) { //item Contain list of Geopints
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));



this works sometimes. However sometimes some points are not shown and I need to then Zoom Out to view those points. Is there any way to solve this problem?


Source: Tips4all

2 comments:

  1. I found out the answer myself, the Zoom level was correct. I need to add following code to display all points on screen.

    objMapController.animateTo(new GeoPoint(
    (maxLatitude + minLatitude)/2,
    (maxLongitude + minLongitude)/2 ));


    The center point was not propery aligned creating problem for me. This works.

    ReplyDelete
  2. Part of the problem could be that MIN_VALUE is still a positive number, but latitudes and longitudes can be negative numbers. Try using NEGATIVE_INFINITY instead of MIN_VALUE.

    ReplyDelete