Login   Register  
Icontem

File: professionals_mapplet.xml

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Manuel Lemos  >  Google Maps KML Overlay  >  professionals_mapplet.xml  >  Download  
File: professionals_mapplet.xml
Role: Example script
Content type: text/plain
Description: Mapplet (Google Maps gadget) that loads list of PHP professionals of different countries into a Google My Maps
Class: Google Maps KML Overlay
Load features from KML file into Google maps
Author: By
Last change:
Date: 2010-09-08 12:32
Size: 7,501 bytes
 

Contents

Class file image Download
<Module> 
<ModulePrefs title="PHP professionals available for hiring"
             description="PHP professionals available for taking PHP jobs"
             author="PHP Classes"
             author_email="info@phpclasses.org"
             screenshot="http://files.phpclasses.org/graphics/professionals_mapplet.png"
             thumbnail="http://files.phpclasses.org/graphics/professionals_mapplet.png"
             directory_title="PHP professionals available for taking PHP jobs"
             height="300"> 
<Require feature="sharedmap" /> 
<Require feature="setprefs" /> 
</ModulePrefs> 
<UserPref name="country"
          default_value=""
          datatype="hidden"
/> 
<UserPref name="zoom"
          default_value="true"
          datatype="bool"
          display_name="Zoom to selected country"
/> 
<Content type="html"><![CDATA[ 
 
><div id="message" style="text-align: center"></div> 
<div style="text-align: center" id="choice"></div> 
<script type="text/javascript"> 
 
var countries = [];
var map;
var geoXml;
var message;
var current_country;
var map_center;
var is_mapplet = true;
var preferences = new _IG_Prefs();
 
function setzoom(zoom)
{
	map.setCenter(new GLatLng((current_country.minimumlatitude + current_country.maximumlatitude) / 2, (current_country.minimumlongitude + current_country.maximumlongitude) / 2), zoom);
}
 
function switchcountry(country)
{
	if(country.length != 2
	|| !(current_country = countries[country]))
		return false;
	message.innerHTML = 'Loading country ' + current_country.name + '...';
	if(geoXml != undefined)
		map.removeOverlay(geoXml);
	var locations = 'http://www.phpclasses.org/professionals/country/' + country + '/locations/locations.kml';
	if(is_mapplet)
		geoXml = new GGeoXml(locations);
	else
	{
		var featured_icon=new GIcon();
		featured_icon.transparent=featured_icon.image='http://files.phpclasses.org/graphics/phpclasses/PHP-featured-marker.png';
		featured_icon.shadow='http://files.phpclasses.org/graphics/phpclasses/PHP-marker-shadow.png';
		featured_icon.iconSize=new GSize(32,32);
		featured_icon.shadowSize=new GSize(49,32);
		featured_icon.iconAnchor=new GPoint(16,32);
		featured_icon.infoWindowAnchor=new GPoint(16,0);
		featured_icon.imageMap=[11,0,21,0,27,2,31,6,31,13,25,18,19,19,17,31,15,31,12,19,0,13,0,6,5,2];
		featured_icon.transparent='http://files.phpclasses.org/graphics/phpclasses/PHP-featured-marker.png';
		var non_featured_icon=new GIcon();
		non_featured_icon.transparent=non_featured_icon.image='http://files.phpclasses.org/graphics/phpclasses/PHP-marker.png';
		non_featured_icon.shadow='http://files.phpclasses.org/graphics/phpclasses/PHP-marker-shadow.png';
		non_featured_icon.iconSize=new GSize(32,32);
		non_featured_icon.shadowSize=new GSize(49,32);
		non_featured_icon.iconAnchor=new GPoint(16,32);
		non_featured_icon.infoWindowAnchor=new GPoint(16,0);
		non_featured_icon.imageMap=[11,0,21,0,27,2,31,6,31,13,25,18,19,19,17,31,15,31,12,19,0,13,0,6,5,2];
		icons = [];
		icons['#featured-professional-placemark'] = featured_icon;
		icons['#professional-placemark'] = non_featured_icon;
		geoXml = new ML.Maps.KMLOverlay(locations, { icons: icons });
	}
	message.innerHTML = 'Adding ' + current_country.name + ' professionals locations';
	var bounds = new GLatLngBounds(new GLatLng(current_country.minimumlatitude, current_country.minimumlongitude), new GLatLng(current_country.maximumlatitude, current_country.maximumlongitude));
	if(preferences.getBool('zoom'))
	{
		if(is_mapplet)
			map.getBoundsZoomLevelAsync(bounds, setzoom);
		else
		{
			var zoom = map.getBoundsZoomLevel(bounds);
			setzoom(zoom);
		}
	}
	map.addOverlay(geoXml);
	message.innerHTML = '';
	preferences.set('country', country);
	return true;
}
 
function buildform(response)
{
	message.innerHTML = 'Building country list...';
	var cn = response.getElementsByTagName('country');
	var codes = [];
	var closest = '';
	var distance = 0;
	var offset = 5.0;
	for(c = 0; c < cn.length; ++c)
	{
		var code = cn[c].getElementsByTagName('code')[0].childNodes[0].nodeValue;
		codes[c] = code;
		countries[code] = {};
		countries[code].name = cn[c].getElementsByTagName('name')[0].childNodes[0].nodeValue;
		countries[code].professionals = cn[c].getElementsByTagName('professionals')[0].childNodes[0].nodeValue;
		if(is_mapplet)
		{
			var center_node = cn[c].getElementsByTagName('center')[0];
			var latitude = center_node.getElementsByTagName('latitude')[0].childNodes[0].nodeValue - map_center.lat();
			var longitude = center_node.getElementsByTagName('longitude')[0].childNodes[0].nodeValue - map_center.lng();
			var d = latitude * latitude + longitude * longitude;
			if(closest.length == 0
			|| d < distance)
			{
				distance = d;
				closest = code;
			}
		}
		var bounds = cn[c].getElementsByTagName('bounds')[0];
		countries[code].minimumlatitude = Math.max(bounds.getElementsByTagName('minimumlatitude')[0].childNodes[0].nodeValue - offset, -90.0);
		countries[code].minimumlongitude = Math.max(bounds.getElementsByTagName('minimumlongitude')[0].childNodes[0].nodeValue - offset, -180.0);
		countries[code].maximumlatitude = Math.min(bounds.getElementsByTagName('maximumlatitude')[0].childNodes[0].nodeValue + offset, 90.0);
		countries[code].maximumlongitude = Math.min(bounds.getElementsByTagName('maximumlongitude')[0].childNodes[0].nodeValue + offset, 180.0);
	}
	var country = preferences.getCountry();
	var set = switchcountry(country);
/*
	if(!set
	&& is_mapplet
	&& (set = switchcountry(closest)))
		country = closest;
	if(!set)
	{
		country = preferences.getString('.country');
		set = switchcountry(country);
	}
*/
	if(!set)
		country = '';
	var options = '<form name="country"><select onchange="switchcountry(this.options[this.selectedIndex].value);"><option' + (country == '' ? ' selected="selected"' : '') + ' value="">Select a country</option>';
	for(c = 0; c < codes.length; ++c)
	{
		code = codes[c];
		options += '<option' + (country == code ? ' selected="selected"' : '') + ' value="' + code + '">' + countries[code].name + ' (' + countries[code].professionals + ')</option>';
	}
	options += '</select></form>';
	var choice = document.getElementById('choice');
	choice.innerHTML = options;
	message.innerHTML = '';
}
 
function loadcountries(center)
{
	message.innerHTML = 'Loading countries...';
	map_center = center;
	_IG_FetchXmlContent('http://www.phpclasses.org/professionals/locations/locations.xml', buildform);
}
 
	try
	{
		GMap2;
	}
	catch(e)
	{
		is_mapplet = false;
		document.writeln('<script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2"></scr' + 'ipt>');
		document.writeln('<script type="text/javascript" src="http://files.phpclasses.org/kmloverlay.js"></scr' + 'ipt>');
		document.writeln('<div id="map" style="height: 264px"></div>');
	}
 
	_IG_RegisterOnloadHandler(function ()
		{
 
 
			message = document.getElementById('message');
			if(is_mapplet)
				map = new GMap2();
			else
			{
				map = new GMap2(document.getElementById('map'));
				map.setCenter(new GLatLng(0, 0), 1);
				map.addControl(new GSmallMapControl());
				map.addControl(new GMapTypeControl());
			}
			if(is_mapplet)
				map.getCenterAsync(loadcountries);
			else
			{
				var center = map.getCenter();
				loadcountries(center);
			}
		}
	);
</script> 
 
]]></Content> 
</Module>