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

3

43

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?

Doom answered 25/2, 2011 at 7:31 Comment(2)
I'm not sure but I think this might be because zooming in the map works with discrete levels. So there might be sets of markers which cannot be all placed on the map without zooming too wide out. Have you tried to set the zoom level per hand for a set of markers where the zoomToSpan() method fails? By this way you can check if it is possible at all. When you cannot archive it by hand I don't think you can archive it by zoomToSpan().Haemophiliac
Yes I had tried that, just by zooming one level up it worked.All those points were just at the border of my screen.Doom
D
31

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.

Doom answered 25/2, 2011 at 10:44 Comment(1)
What object is objMapController ? How to create this reference can you post it ?Nimrod
C
69

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));
}
Corpuz answered 26/12, 2012 at 2:5 Comment(1)
Thanks, I was looking for something like this. Almost felt for the temptation to calculate Min & Max of Point. You saved my day!Gantlet
D
31

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.

Doom answered 25/2, 2011 at 10:44 Comment(1)
What object is objMapController ? How to create this reference can you post it ?Nimrod
L
0

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.

Lepidosiren answered 12/10, 2011 at 18:29 Comment(1)
Not true, it is a negative number. See developer.android.com/reference/java/lang/…: -2147483648, or -2^31.Gizmo

© 2022 - 2024 — McMap. All rights reserved.