function load( address ) { if ( GBrowserIsCompatible() ) { // array to hold copies of the markers and html //instantiate the map, add the controls we want, set the center point and zoom position. var map = new GMap2( document.getElementById( "map" ) ); var markerManager = null; //zoom levels 0-17+, 0 == world var zoomLevel = 0; var homeMarker = null; var home_marker_html = 'You are here.
' + address; function getViewableMarkers( markers ){ var viewableMarkers = 0; var mapBounds = null; mapBounds = map.getBounds(); for( var i = 0; i < markers.length; i++ ){ if( mapBounds.contains( markers[i].getPoint() ) ){ viewableMarkers++; } } return viewableMarkers; } function setZoomLevel( markers ){ var low = 0; var high = 17; var viewableMarkers = markers.length; var minMarkersZoomLevel = null; var minMarkers = markers.length; // avoid showing the map while adjusting the zoom level. document.getElementById( "map" ).style.display = "none"; // if low and high have crossed, then we want to set the // zoom level to the zoom level that had the fewest markers // visible. setZoom returns false, so it will also // evaluate to false and the loop will terminate. while( low <= high || map.setZoom( minMarkersZoomLevel ) ){ viewableMarkers = 0; //zoomLevel = parseInt( ( low + high ) / 2 ); zoomLevel = high; map.setZoom( zoomLevel ); viewableMarkers = getViewableMarkers( markers ); // two markers, one is home and one is a retailer // our work here is done. if( viewableMarkers == 2 ){ break; } else if( viewableMarkers <= minMarkers && viewableMarkers > 1 ){ // too many markers visible, so zoom in low = zoomLevel + 1; minMarkers = viewableMarkers; minMarkersZoomLevel = zoomLevel; } else{ // not enough markers visible, so zoom out high = zoomLevel - 1; } /* if( !confirm( "Continue the loop? ( zoomLevel = " + zoomLevel + " : " + low + "," + high + " : " + "viewableMarkers = " + viewableMarkers + ", minMarkers = " + minMarkers + ")" )){ break; } */ } document.getElementById( "map" ).style.display = "block"; } function setMarkers( data, responseCode ) { var xml = GXml.parse( data ); var markerTags = xml.documentElement.getElementsByTagName( "marker" ); var markers = new Array(); var lat = null; var lng = null; var point = null; var title = null; var titleTags = null; var addressTags = null; var street = null; var city = null; var state = null; var zip = null; var html = null; for( var i = 0; i < markerTags.length; i++ ) { //only create and set the marker if it exists. //note: we are exploiting a couple of un-obvious things about javascript to accomplish this check //1: nan and undefined are evaluated as false //2: any non-zero number returned will be evaluated as true. //to catch points exactly on the equator or prime-meridian(0 lat or lng) additional logic is included. if((parseFloat(markerTags[i].getAttribute('lat')) || parseFloat(markerTags[i].getAttribute('lat')) == 0) && (parseFloat( markerTags[i].getAttribute('lng')) || parseFloat( markerTags[i].getAttribute('lng')) == 0)) { // obtain the attribues of each marker lat = parseFloat( markerTags[i].getAttribute( 'lat' ) ); lng = parseFloat( markerTags[i].getAttribute( 'lng' ) ); point = new GLatLng( lat,lng ); titleTags = markerTags[i].getElementsByTagName( "title" ); addressTags = markerTags[i].getElementsByTagName( "address" ); //there is only one title tag and address tag per marker title = titleTags[0].firstChild.nodeValue; street = addressTags[0].getAttribute( 'street' ); city = addressTags[0].getAttribute( 'city' ); state = addressTags[0].getAttribute( 'state' ); zip = addressTags[0].getAttribute( 'zip' ); html = '' + title + '
' + street + '
' + city + ', ' + state + ' ' + zip; // create the marker markers[i] = createMarker( point, html ); } } //append the homeMarker to the end of the markers array. markers[markers.length] = homeMarker; //add the markers to the marker manager markerManager.addMarkers( markers, 0 ); setZoomLevel( markers ); homeMarker.openInfoWindowHtml( home_marker_html ); } function setHomePoint( home_point ) { if ( !home_point ) { alert( address + " not found" ); } else { map.setCenter( home_point, zoomLevel ); markerManager = new GMarkerManager( map, { borderPadding: 0 } ); homeMarker = createMarker( home_point, home_marker_html, true ); GDownloadUrl( 'locations.xml', setMarkers ); } } // A function to create the marker and set up the event window and tie it to the click event. function createMarker( point, html ){ createMarker( point, html, false ); } function createMarker( point, html, is_home ) { if( is_home ) { var icon = new GIcon(); icon.image = "images/home.png"; icon.iconSize = new GSize( 20, 20 ); icon.iconAnchor = new GPoint( 6, 20 ); icon.infoWindowAnchor = new GPoint( 5, 1 ); var marker = new GMarker( point, icon ); } else { var marker = new GMarker( point ); } //add the onclick event to this marker. GEvent.addListener( marker, "click", function( ) { //onclick, open the infoWindow. marker.openInfoWindowHtml( html ); }); return marker; } //create geocoder object var geocoder = new GClientGeocoder(); geocoder.getLatLng( address, setHomePoint ); //now add our controls map.addControl( new GSmallMapControl( ) ); map.addControl( new GMapTypeControl( ) ); map.addControl( new GScaleControl( ) ); // map.addControl( new GOverviewMapControl( ) ); } else { alert( "Sorry, the Google Maps API is not compatible with this browser" ); } }