Click here to Skip to main content
15,886,067 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
<?php
/**
 * A query printer for maps using the Yahoo Maps API
 *
 * @file SM_YahooMaps.php
 * @ingroup SemanticMaps
 *
 * @author Jeroen De Dauw
 */

if( !defined( 'MEDIAWIKI' ) ) {
	die( 'Not an entry point.' );
}

final class SMYahooMaps extends SMMapPrinter {
	// TODO: create a class instead of a code horror :D

	public function getName() {
		wfLoadExtensionMessages('SemanticMaps');
		return wfMsg('sm_yahoomaps_printername');
	}

	/**
	 * Returns the Yahoo Map Control type for the provided a general map control
	 * type. When no match is found, the provided control name will be used.
	 */
	public static function getExtraMapControls($controls, $yahooMapsOnThisPage) {
		global $egMapsYMapControls;
		
		$extraMapControls = '';
		$panAdded = false; $zoomAdded = false;
		
		if (count($controls) < 1) $controls = $egMapsYMapControls; // In case no controls are provided, use the default
		
		foreach ($controls as $control) { // Loop through the controls, and add the JS needed to add them
			switch (strtolower($control)) {
				case 'pan' : 
					if (!$panAdded) {$extraMapControls .= "yahoo_$yahooMapsOnThisPage.addPanControl(); "; $panAdded = true; }
					break;				
				case 'zoom' : 
					if (!$zoomAdded) {$extraMapControls .= "yahoo_$yahooMapsOnThisPage.addZoomLong(); "; $zoomAdded = true; }
					break;
			}
		}
		
		return $extraMapControls;
	}	
	
	protected function getResultText($res, $outputmode) {
		parent::getResultText($res, $outputmode);
		
		// Go through the array with map parameters and create new variables
		// with the name of the key and value of the item.
		foreach($this->m_params as $paramName => $paramValue) {
			if (empty(${$paramName})) ${$paramName} = $paramValue;
		}
		
		$result = "";
		
		global $egYahooMapsOnThisPage, $egMapsYahooMapsZoom;
		
		if (strlen($zoom) < 1) $zoom = $egMapsYahooMapsZoom;
		
		if (empty($egYahooMapsOnThisPage)) {
			$egYahooMapsOnThisPage = 0;
			MapsYahooMaps::addYMapDependencies($result);	
		}
		
		$egYahooMapsOnThisPage++;
		
		// Get the Yahoo Maps names for the control and map types
		$type = MapsYahooMaps::getYMapType($type);
		$extraMapControls = self::getExtraMapControls($controls, $egYahooMapsOnThisPage);
		
		$map_text = "";
		
		if (count($this->m_locations) > 0) {
			if (empty($centre)) {
				// If the center is not set, it needs to be determined, together with the bounds
				// This is done with the getBestZoomAndCenter function of the Y! Maps API
				$map_text .= "var ymap_locations_$egYahooMapsOnThisPage = Array();";
				
				foreach ($this->m_locations as $i => $location) {
					// Add the markers to the map
					list($lat, $lon, $title, $label, $icon) = $location;
					$title = str_replace("'", "\'", $title);
					$label = str_replace("'", "\'", $label);
					$map_text .= "
						yahoo_$egYahooMapsOnThisPage.addOverlay(createYMarker(new YGeoPoint($lat, $lon), '$title', '$label'));
						ymap_locations_$egYahooMapsOnThisPage.push(new YGeoPoint($lat, $lon));";
				}
				
				$map_text .= "var centerAndZoom = yahoo_$egYahooMapsOnThisPage.getBestZoomAndCenter(ymap_locations_$egYahooMapsOnThisPage); 
				yahoo_$egYahooMapsOnThisPage.drawZoomAndCenter(centerAndZoom.YGeoPoint, centerAndZoom.zoomLevel);";
			}
			else {
				//if ($centre == null) {
				//	$centre_lat = 0;
				//	$centre_lon = 0;
				//}
				//else {
					// If the center is set, get the coordinates
					list($centre_lat, $centre_lon) = MapsUtils::getLatLon($centre);
				//}
				
				foreach ($this->m_locations as $i => $location) {
					// Add the markers to the map
					list($lat, $lon, $title, $label, $icon) = $location;
					$title = str_replace("'", "\'", $title);
					$label = str_replace("'", "\'", $label);
					$map_text .= "yahoo_$egYahooMapsOnThisPage.addOverlay(createYMarker(new YGeoPoint($lat, $lon), '$title', '$label'));";
				}
				
				$map_text .= "	yahoo_$egYahooMapsOnThisPage.drawZoomAndCenter(new YGeoPoint($centre_lat, $centre_lon), $zoom);";
			}
		}		
		
		// Disbale the scroll wheel zoom when autozoom is set to off
		switch($autozoom) {
			case 'no' : case 'off' : 
				$disbaleKeyControlCode = "yahoo_$egYahooMapsOnThisPage.disableKeyControls();";
				break;
			default:
				$disbaleKeyControlCode = '';
				break;
		}		
		
		$width = $width . 'px';
		$height = $height . 'px';		
		
		$result .= "
		<div id='map-yahoo-$egYahooMapsOnThisPage' style='width: $width; height: $height;'></div>  
		
		<script type='text/javascript'>/*<![CDATA[*/
		var yahoo_locations_$egYahooMapsOnThisPage = new YGeoPoint($lat, $lon);
		var yahoo_$egYahooMapsOnThisPage = new YMap(document.getElementById('map-yahoo-$egYahooMapsOnThisPage'));
		yahoo_$egYahooMapsOnThisPage.addTypeControl(); $extraMapControls 
		yahoo_$egYahooMapsOnThisPage.setMapType($type); 
		$disbaleKeyControlCode $map_text /*]]>*/</script>";
		
		return array($result, 'noparse' => 'true', 'isHTML' => 'true');
	}


}

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