Skip to content

Instantly share code, notes, and snippets.

@kuratowsky
Last active March 15, 2017 00:43
Show Gist options
  • Save kuratowsky/2575646 to your computer and use it in GitHub Desktop.
Save kuratowsky/2575646 to your computer and use it in GitHub Desktop.
This function creates Gmaps v3 bounds
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));
}
@kuratowsky
Copy link
Author

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"
}
];

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