Click here to Skip to main content
15,885,546 members
Articles / Database Development / MySQL

Semantic Maps

Rate me:
Please Sign up or sign in to vote.
4.55/5 (5 votes)
1 Aug 2009GPL313 min read 36.8K   1.4K   23  
A MediaWiki extension that allows you to insert, edit, view and aggregate coordinate data
 /**
  * Javascript functions for Yahoo! Maps functionallity in Maps and it's extensions
  *
  * @file YahooMapFunctions.js
  * @ingroup Maps
  *
  * @author Jeroen De Dauw
  */


/**
 * Returns YMarker object on the provided location.
 * It will show a popup baloon with title and label when clicked, if either of these is set.
 */
function createYMarker(geoPoint, title, label){
	var newMarker= new YMarker(geoPoint);
	
	if ((title + label).length > 0) {
		var bothTxtAreSet = title.length > 0 && label.length > 0;
		var markerMarkup = bothTxtAreSet ? '<b>' + title + '</b><hr />' + label : title + label;
		YEvent.Capture(newMarker, EventsList.MouseClick, 
			function(){
				newMarker.openSmartWindow(markerMarkup);
			}
		);
	}

	return newMarker;
}

/**
 * Returns YMap object with the provided properties and markers.
 */
function initializeYahooMap(mapName, lat, lon, zoom, type, controls, scrollWheelZoom, markers) {
	return createYahooMap(document.getElementById(mapName), new YGeoPoint(lat, lon), zoom, type, controls, scrollWheelZoom, markers);
}

/**
 * Returns YMap object with the provided properties.
 */
function createYahooMap(mapElement, centre, zoom, type, controls, scrollWheelZoom, markers) {
	var map = new YMap(mapElement); 
	
	map.addTypeControl();
	
	for (i in controls){
		switch (controls[i]) {
			case 'pan' :
				map.addPanControl();
				break;
			case 'zoom' : 
				map.addZoomLong();
				break;
		}
	}	
	
	map.setMapType(type);
	
	if (!scrollWheelZoom) map.disableKeyControls();
	
	for (i in markers) {
		var marker = markers[i];
		map.addOverlay(createYMarker(marker.point, marker.title, marker.label));
	}		
	
	// FIXME: the code after this line REFUSES to be executed
	// This is probably caused by the YGeoPoint
	// Notice that the map object will therefore NOT BE RETURNED!
	map.drawZoomAndCenter(centre , zoom);
	
	return map;
}

/**
 * This function holds spesific functionallity for the Yahoo! Maps form input of Semantic Maps
 * TODO: Refactor as much code as possible to non specific functions
 */
function makeFormInputYahooMap(mapName, locationFieldName, lat, lon, zoom, marker_lat, marker_lon, type, controls, scrollWheelZoom) {
	var map = createYahooMap(document.getElementById(mapName), new YGeoPoint(lat, lon), zoom, type, controls, scrollWheelZoom);

	// Show a starting marker only if marker coordinates are provided
	if (marker_lat != null && marker_lon != null) {
		map.addOverlay(createYMarker(new YGeoPoint(marker_lat, marker_lon)));
	}
	
	// Click event handler for updating the location of the marker
		YEvent.Capture(map, EventsList.MouseClick,
		function(_e, point) {
			var loc = new YGeoPoint(point.Lat, point.Lon)
			map.removeMarkersAll();
			document.getElementById(locationFieldName).value = convertLatToDMS(point.Lat)+', '+convertLngToDMS(point.Lon);
			map.addMarker(loc);
			map.panToLatLon(loc);
		}
	);
	
	// Make the map variable available for other functions
	if (!window.YMaps) window.YMaps = new Object;
	eval("window.YMaps." + mapName + " = map;"); 
}

/**
 * This function holds spesific functionallity for the Yahoo! Maps form input of Semantic Maps
 * TODO: Refactor as much code as possible to non specific functions
 */
function showYAddress(address, mapName, outputElementName, notFoundFormat) {
	var map = YMaps[mapName];
	
	map.removeMarkersAll();
	map.drawZoomAndCenter(address);
	
	YEvent.Capture(map, EventsList.onEndGeoCode,
		function(resultObj) {
			map.addOverlay(new YMarker(resultObj.GeoPoint));
			document.getElementById(outputElementName).value = convertLatToDMS(resultObj.GeoPoint.Lat) + ', ' + convertLngToDMS(resultObj.GeoPoint.Lon);				
		}
	);
}
 
function getYMarkerData(lat, lon, title, label, icon) {
		return {point: new YGeoPoint(lat, lon), title: title, label: label, icon: icon};
	}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
Belgium Belgium
I am a free and open source software enthusiast and freelance software developer with multiple years of experience in both web and desktop development. Currently my primarily focus is on MediaWiki and Semantic MediaWiki work. I'm in the all time top 10 MediaWiki comitters and am one of the WikiWorks consultants. You can contact me at jeroendedauw at gmail for development jobs and questions related to my work.

More info can be found on my website [0] and my blog [1]. You can also follow me on twitter [2] and identi.ca [3].

[0] http://www.jeroendedauw.com/
[1] http://blog.bn2vs.com/
[2] https://twitter.com/#!/JeroenDeDauw
[3] http://identi.ca/jeroendedauw

Comments and Discussions