Map: Fit Region automatical
Hi,
is there a way to set my Map region dynamical by the annotations that are on my map?
E.g. i got 5 Annotations and i want my map to fit the screen so all 5 are visible in the window.
3 Answers
-
Hi,
I've create this function which take an array of points and return a region to set the map location.
findZoomRegion : function(points) { var tmpDeltatLat = 0, tmpDeltatLong = 0, maxDeltatLat = 0, maxDeltatLong = 0, centerLat = 0, centerLong = 0; for(var i = 0; i <= Math.floor(points.length / 2); i++) { for(var j = nbPtToShow; j >= Math.floor(points.length / 2); j--) { if(j != i) { tmpDeltatLat = Math.abs(Math.abs(points[i].latitude) - Math.abs(points[j].latitude)); if(tmpDeltatLat > maxDeltatLat) { maxDeltatLat = tmpDeltatLat; centerLat = Math.min(points[i].latitude, points[j].latitude) + maxDeltatLat / 2; } tmpDeltatLong = Math.abs(Math.abs(points[i].longitude) - Math.abs(points[j].longitude)); if(tmpDeltatLong > maxDeltatLong) { maxDeltatLong = tmpDeltatLong; centerLong = Math.min(points[i].longitude, points[j].longitude) + maxDeltatLong / 2; } } } } var region = { latitude : centerLat, longitude : centerLong, latitudeDelta : maxDeltatLat, longitudeDelta : maxDeltatLong }; return region; }
I've got some issue on Android, cause the map zoom level is round up or down.
-
Hey all,
There's a few comments etc around the errors in the original code, but it's a little hard to follow, so here is the working version of the above function in a gist:
https://gist.github.com/mattlanham/6233976
Upvote this answer if you found it useful! Hope you enjoy!
-
I'm wondering if it would is just be a matter of getting the max lat and long values of all the annotations in your set and then set the region boundaries to that? I don't have code for how to do this, but I think that's how it would be pulled off. Does anyone have a code example of this?