Fit bounds

This commit is contained in:
Erik Michaels-Ober 2011-03-17 19:09:59 -07:00
parent 379c137bbd
commit a5a5e0ada6
1 changed files with 29 additions and 11 deletions

View File

@ -1,15 +1,15 @@
$(function() { $(function() {
var latlng = new google.maps.LatLng(42.358431, -71.059773); var center = new google.maps.LatLng(42.358431, -71.059773);
var southWest; var southWest;
var northEast; var northEast;
var bounds; var bounds;
var zoomLevel = 15;
var myOptions = { var myOptions = {
center: latlng, center: center,
mapTypeControl: false, mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeId: google.maps.MapTypeId.ROADMAP,
panControl: false, panControl: false,
zoom: 15, zoom: zoomLevel
zoomControl: false
}; };
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var activeHydrantId; var activeHydrantId;
@ -17,6 +17,9 @@ $(function() {
var activeInfoWindow; var activeInfoWindow;
var hydrantIds = []; var hydrantIds = [];
function addMarker(hydrantId, point, color) { function addMarker(hydrantId, point, color) {
if($.inArray(hydrantId, hydrantIds) != -1) {
return;
}
var image = new google.maps.MarkerImage(color, var image = new google.maps.MarkerImage(color,
new google.maps.Size(27.0, 37.0), new google.maps.Size(27.0, 37.0),
new google.maps.Point(0, 0), new google.maps.Point(0, 0),
@ -66,23 +69,38 @@ $(function() {
$('#address').addClass('error', 500); $('#address').addClass('error', 500);
$('#address').focus(); $('#address').focus();
} else { } else {
center = new google.maps.LatLng(lat, lng); var northMost;
map.setCenter(center) var eastMost;
map.setZoom(18); var southMost;
var westMost;
var i = 0; var i = 0;
$(data).each(function(index, hydrant) { $(data).each(function(index, hydrant) {
hydrant = hydrant.hydrant; hydrant = hydrant.hydrant;
if($.inArray(hydrant.id, hydrantIds) != -1) { if(!northMost || northMost > hydrant.lat) {
// continue northMost = hydrant.lat;
return true; }
if(!eastMost || eastMost > hydrant.lng) {
eastMost = hydrant.lng;
}
if(!southMost || southMost < hydrant.lat) {
southMost = hydrant.lat;
}
if(!westMost || westMost < hydrant.lng) {
westMost = hydrant.lng;
} }
setTimeout(function() { setTimeout(function() {
point = new google.maps.LatLng(hydrant.lat, hydrant.lng); point = new google.maps.LatLng(hydrant.lat, hydrant.lng);
color = '/images/markers/' + (hydrant.user_id ? 'green' : 'red') + '.png'; color = '/images/markers/' + (hydrant.user_id ? 'green' : 'red') + '.png';
addMarker(hydrant.id, point, color); addMarker(hydrant.id, point, color);
}, i * 100); }, i * 100);
i += 1 if($.inArray(hydrant.id, hydrantIds) == -1) {
i += 1;
}
}); });
southWest = new google.maps.LatLng(southMost, westMost);
northEast = new google.maps.LatLng(northMost, eastMost);
bounds = new google.maps.LatLngBounds(southWest, northEast);
map.fitBounds(bounds);
} }
}); });
} }