Skip to content

Instantly share code, notes, and snippets.

@mattlanham
Created August 14, 2013 18:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattlanham/6233976 to your computer and use it in GitHub Desktop.
Save mattlanham/6233976 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

@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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment