Last active
March 15, 2017 00:43
-
-
Save kuratowsky/2575646 to your computer and use it in GitHub Desktop.
This function creates Gmaps v3 bounds
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 createBounds(list){ | |
var maxlat=null, maxlng=null, minlat=null, minlng=null; | |
jQuery(list).each(function (index, elem) { | |
try { | |
if (elem) { | |
var lt = parseFloat(elem.lat), lg = parseFloat(elem.lng); | |
if (lt != 0 || lg != 0 || !isNaN(lt) || !isNaN(lg)) { | |
maxlat = (maxlat==null)?lt:Math.max(maxlat, lt); | |
maxlng = (maxlng==null)?lg:Math.max(maxlng, lg); | |
minlat = (minlat==null)?lt:Math.min(minlat, lt); | |
minlng = (minlng==null)?lg:Math.min(minlng, lg); | |
} | |
} | |
} catch (err) { | |
throw "ERROR! :" + err; | |
} | |
}); | |
return new google.maps.LatLngBounds(new google.maps.LatLng(minlat,minlng), new google.maps.LatLng(maxlat,maxlng)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The var list is an Array of JSON items with this structure:
hotels = [
{
lat: -33.85046783834696,
lng: 151.21843479573727,
name: "Hotel-1 Name",
catCode: "Cat3",
id: "123"
},
{
lat: -33.87100014835596,
lng: 151.22224990278482,
name: "Hotel-2 Name",
catCode: "Cat3",
id: "456"
}
];