Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.