Created
November 18, 2010 16:59
-
-
Save devth/705276 to your computer and use it in GitHub Desktop.
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(){ | |
var days = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday']; | |
var bounds = new google.maps.LatLngBounds(); | |
var visible_markers = []; | |
var neighborhood_markers = {}; | |
var seattle = new google.maps.LatLng(47.61680985980715, -122.34203338623047); | |
var options = { | |
zoom: 12, | |
center: seattle, | |
disableDefaultUI: true, | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}; | |
map = new google.maps.Map(document.getElementById("map_box"), options); | |
$("#top_nav a").toggle(function(e){ | |
return false; | |
}, function(e){ | |
return false; | |
}); | |
$("#left_nav a").toggle(function(e){ | |
var a = $(this); | |
a.addClass('active'); | |
var id = a.data('id'); | |
var neighborhood_data = neighborhoods_by_id[id]; | |
// map.setCenter( new google.maps.LatLng(neighborhood_data.latitude, neighborhood_data.longitude) ); | |
// map.setZoom( neighborhood_data.zoom ); | |
if (neighborhood_markers[id] == null){ | |
neighborhood_markers[id] = []; | |
for (address_index in neighborhood_data.addresses){ | |
var address = neighborhood_data.addresses[address_index]; | |
var spot = address.spot; | |
var marker = new google.maps.Marker({ | |
position: new google.maps.LatLng(address.latitude, address.longitude), | |
map: map, | |
title: spot.name, | |
icon: new google.maps.MarkerImage('images/icon_truck.png') | |
}); | |
marker.address = address; | |
show_marker(marker); | |
neighborhood_markers[id].push(marker); | |
} | |
} else { | |
$.each(neighborhood_markers[id], function(index,marker){ | |
show_marker(marker); | |
}); | |
} | |
update_bounds(); | |
return false; | |
}, function(e){ | |
var a = $(this); | |
a.removeClass('active'); | |
var id = a.data('id'); | |
$.each(neighborhood_markers[id], function(index,marker){ | |
hide_marker(marker); | |
}); | |
update_bounds(); | |
return false; | |
}); | |
function show_marker(marker){ | |
marker.setMap(map); | |
visible_markers.push(marker); | |
} | |
function hide_marker(marker){ | |
visible_markers.splice($.inArray(marker), 1); | |
marker.setMap(null); | |
} | |
function update_bounds(){ | |
console.log("ub!"); | |
// UPDATE enabled days nav | |
var days_enabled = {}; | |
$.each(days, function(i, day){ | |
days_enabled[day] = false; | |
}); | |
$("#top_nav a").removeClass('active').addClass('disabled'); | |
$.each(visible_markers, function(i,marker){ | |
$.each(days,function(i,day){ | |
if (marker.address[day] === true) days_enabled[day] = true; | |
}); | |
}); | |
$.each(days_enabled, function(day, enabled){ | |
if (enabled === true) $("#top_nav #" + day).removeClass('disabled'); | |
}); | |
// UPDATE bounds | |
bounds = new google.maps.LatLngBounds(); | |
$.each(visible_markers, function(index,marker){ | |
bounds.extend(marker.getPosition()); | |
}); | |
if (visible_markers.length > 0){ | |
// map.fitBounds(bounds); | |
setTimeout( function() { | |
map.fitBounds(bounds); | |
var max_zoom = neighborhoods_by_id[$('#left_nav a.active').data('id')].zoom; | |
if (map.getZoom() > max_zoom) map.setZoom(max_zoom); | |
}, 1); | |
}else { | |
map.setCenter(options.center); | |
map.setZoom(options.zoom); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment