Newer
Older
Digital_Repository / Repositories / Maps / google_map.js
nstanger on 12 May 2006 2 KB - Added PHP to calling web pages.
function load( startTime, numEntries )
{
	// Page initialisation time.
	var startTime = new Date( startTime );
	
	// Somewhere to write the timing information to.
	var timer = document.getElementById( "timer" );
	timer.innerHTML = "Loading...";
	
	if ( GBrowserIsCompatible() )
	{
		// Create new map.
		var map = new GMap2( document.getElementById( "map" ) );
		map.addControl( new GSmallMapControl() );
		map.addControl( new GMapTypeControl() );
		map.setCenter( new GLatLng(10, 0), 2 );
	
		// Creates a marker at the given point with a label
		// containing the other remaining arguments.
		function createMarker( point, city, abstracts, downloads )
		{
			var marker = new GMarker( point );
			GEvent.addListener( marker, "click",
				function()
				{
					marker.openInfoWindowHtml( "<strong>" + city + "</strong> (" +
						abstracts + " abstracts, " +
						downloads + " downloads)" );
				} );
			return marker;
		}
	
		// Download the data in google_map_data.xml and load it on the map.
		// The format we expect is:
		// <markers abs="200" dl="100" ips="27">
		//   <marker city="Timbuktu" lat="37.441" long="-122.141" abs="10" dl="0" />
		//   <marker city="West Bromwich" lat="37.322" long="-121.213" abs="22" dl="49" />
		// </markers>
		GDownloadUrl( "google_map_generate_data.pl?top=" + numEntries,
			function( data, responseCode )
			{
				var xml = GXml.parse( data );
				var markerlist = xml.documentElement.getElementsByTagName( "marker" );
				for ( var i = 0; i < markerlist.length; i++ )
				{
					var point = new GLatLng( parseFloat( markerlist[i].getAttribute( "lat" ) ),
						parseFloat( markerlist[i].getAttribute( "long" ) ) );
					map.addOverlay( createMarker( point, markerlist[i].getAttribute( "city" ),
						markerlist[i].getAttribute( "abs" ), markerlist[i].getAttribute( "dl" ) ) );
				}
				// Grab finishing time. We have to do this here rather than in
				// the main part of the load() function because the map data
				// are loaded asynchronously.
				var endTime = new Date();
				var numUA = xml.documentElement.getAttribute( "ua" );
				var numUD = xml.documentElement.getAttribute( "ud" );
				timer.innerHTML = '<span style="color:red;">' +
					xml.documentElement.getAttribute( "dl" ) +
					' downloads' + ( ( numUD > 0 ) ? ( ' (' + numUD + ' unmappable)' ) : '' ) +
					'</span> & <span style="color:blue;">' +
					xml.documentElement.getAttribute( "abs" ) +
					' abstracts' + ( ( numUD > 0 ) ? ( ' (' + numUA + ' unmappable)' ) : '' ) +
					'</span> from ' +
					markerlist.length +
					' cities (' +
					xml.documentElement.getAttribute( "ips" ) +
					' IP addresses), page generated in ' +
					( endTime - startTime ) +
					' ms.';
			} );
	}
}