Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 633
Tushar Vengurlekar Avatar asked Feb 25 '11 07:02

Tushar Vengurlekar


People also ask

How do you zoom all the way in on Google Maps?

Zoom in the map Double tap a spot on the map, and then: Drag down to zoom in. Drag up to zoom out.

How do I change the max zoom level in Google Maps?

Setting up the minimum or maximum Zoom value for Google Maps can be done by adding the shortcode attribute with Store Locator Shortcode. Furthermore, the maximum value of Google Maps Zoom level is 22. Therefore, the defined maximum value must be below or equal to 22, which is the default.

How do I move a marker smoothly on Google Maps?

The transition() and moveMarker() are used to move marker smoothly on click on the Google map.


2 Answers

Yet another approach with 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));
}
like image 92
iutinvg Avatar answered Oct 18 '22 06:10

iutinvg


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.

like image 37
Tushar Vengurlekar Avatar answered Oct 18 '22 06:10

Tushar Vengurlekar