Created
August 14, 2013 18:26
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
@iantearle, multiply your maxDeltaLat/Long by 2 and you'll get the zoom level to default to the next higher level. So...