Instantly share code, notes, and snippets.

# mattlanham/gist:6233976

Created Aug 14, 2013
Working version of findZoomRegion, alloying you to pass an array of points and find the best region lat/lng for the mapview in Appcelerator Titanium
 function findZoomRegion(points) { var nbPtToShow = points.length-1; 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; }

### iantearle commented Oct 2, 2013

 Is there any way to get this to go a step outwards? I am finding that the delta isnt enough to actually see the markers on the screen.

### afilbert commented Apr 22, 2015

 @iantearle, multiply your maxDeltaLat/Long by 2 and you'll get the zoom level to default to the next higher level. So... ``````var region = { latitude : centerLat, longitude : centerLong, latitudeDelta : maxDeltatLat*2, longitudeDelta : maxDeltatLong*2 }; ``````

### wackyapps commented Oct 8, 2015

 When i apply this code and pass array of points having latitude and longitude with 14 points this throw error: Cannot read property '0' or undefined. The same js I used in browser works fine.