/** * The map object, null until script loads in. * @type {GMap2} */ var map = null; /** * The bounds of the markers once loaded in. * @type {GLatLngBounds} */ var bounds = null; /** * The marker with currently opened info window. * @type {GMarker} */ var currentMarker = null; /** * The dom element that the map is loaded into * @type {Element} */ var mapDiv = null; /** * The dom element that everything is a child of. * @type {Element} */ var containerDiv = null; /** * Position of mouse click (clientX) on map div when in static mode. * @type {Number} */ var clickedX = 0; /** * Position of mouse click (clientY) on map div when in static mode. * @type {Number} */ var clickedY = 0; /** * Indicates whether we've created a script tag with Maps API yet * @type {Boolean} */ var isLoaded = false; /** * Called after script is asynchronously loaded in. * Creates the GMap2, GMarker objects and performs actions according to * what the user did to trigger the map load (search, zoom, click etc). */ function loadMap() { if (GBrowserIsCompatible()) { mapDiv.style.background = '#fff'; mapDiv.style.cursor = ''; map = new GMap2(mapDiv, {logoPassive: true}); map.addControl(new GLargeMapControl()); map.enableScrollWheelZoom(); bounds = new GLatLngBounds(); for (var i = 0; i < businesses.length; i++) { bounds.extend(new GLatLng(businesses[i].lat, businesses[i].lng)); } var latSpan = bounds.toSpan().lat(); map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); // The static map server gives markers more space when calculating // bounds and zoom level, so sometimes the API will give a higher // zoom level than was used by the static map server. // The .98 value is just a guess right now, may need tweaking. var newBounds = map.getBounds(); var newLatSpan = newBounds.toSpan().lat(); if (latSpan/newLatSpan > .90) { map.zoomOut(); } for (var i = 0; i < businesses.length; i++) { var marker = createMarker(i); var latlng = marker.getLatLng(); var pixel = map.fromLatLngToDivPixel(latlng); if (Math.abs(pixel.x - clickedX) < 12 && Math.abs(pixel.y - clickedY) < 20) { //GEvent.trigger(marker, 'click'); } map.addOverlay(marker); } } } /** * Zooms to the viewport that fits all the markers. */ function zoomToAll() { map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); } /** * Creates a marker for the given business. * @param {Number} ind * @return {GMarker} */ function createMarker(ind) { var business = businesses[ind]; var marker = new GMarker(new GLatLng(business.lat, business.lng)); GEvent.addListener(marker, 'click', function() { marker.html = [ '