Click here to Skip to main content
13,139,132 members (74,093 online)
Click here to Skip to main content

Stats

26.5K views
1.3K downloads
23 bookmarked
Posted 1 Aug 2009

Semantic Maps

, 1 Aug 2009
A MediaWiki extension that allows you to insert, edit, view and aggregate coordinate data
SemanticMaps
COPYING
GoogleMaps
INSTALL
OpenLayers
README
RELEASE-NOTES
YahooMaps
Maps
COPYING
Geocoders
GoogleMaps
INSTALL
OpenLayers
OpenLayers
img
blank.gif
cloud-popup-relative.png
drag-rectangle-off.png
drag-rectangle-on.png
east-mini.png
layer-switcher-maximize.png
layer-switcher-minimize.png
marker-blue.png
marker-gold.png
marker-green.png
marker.png
measuring-stick-off.png
measuring-stick-on.png
north-mini.png
panning-hand-off.png
panning-hand-on.png
slider.png
south-mini.png
west-mini.png
zoom-minus-mini.png
zoom-plus-mini.png
zoom-world-mini.png
zoombar.png
theme
default
img
add_point_off.png
add_point_on.png
blank.gif
close.gif
drag-rectangle-off.png
drag-rectangle-on.png
draw_line_off.png
draw_line_on.png
draw_point_off.png
draw_point_on.png
draw_polygon_off.png
draw_polygon_on.png
editing_tool_bar.png
move_feature_off.png
move_feature_on.png
navigation_history.png
overview_replacement.gif
pan-panel-NOALPHA.png
pan-panel.png
panning-hand-off.png
panning-hand-on.png
pan_off.png
pan_on.png
remove_point_off.png
remove_point_on.png
ruler.png
save_features_off.png
save_features_on.png
view_next_off.png
view_next_on.png
view_previous_off.png
view_previous_on.png
zoom-panel-NOALPHA.png
zoom-panel.png
OSM
README
RELEASE-NOTES
YahooMaps
<?php

/**
 * File containing the MapsGeocoder class which handles the non specific geocoding tasks
 *
 * {{#geocode:<Address>|<param1>=<value1>|<param2>=<value2>}}
 * {{#geocodelat:<Address>|<param1>=<value1>|<param2>=<value2>}}
 * {{#geocodelng:<Address>|<param1>=<value1>|<param2>=<value2>}}
 *
 * @file Maps_Geocoder.php
 * @ingroup Maps
 *
 * @author Jeroen De Dauw
 * @author Sergey Chernyshev
 */

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

final class MapsGeocoder {
	// TODO: some refactoring: the arrays containing the result should be generalized - currently only logical for the Google Geocoder service

	private static $mEnableCache = true;
	private static $mGeocoderCache = array();

	public static function renderGeocoder(&$parser, $address, $service = '', $mappingService = '') {
		$geovalues = MapsGeocoder::geocode($address, $service, $mappingService);
		return $geovalues ? $geovalues[2].', '.$geovalues[3] : '';
	}

	public static function renderGeocoderLat(&$parser, $address, $service = '') {
		$geovalues = MapsGeocoder::geocode($address, $service);
		return $geovalues ? $geovalues[2] : '';
	}

	public static function renderGeocoderLng(&$parser, $address, $service = '') {
		$geovalues = MapsGeocoder::geocode($address, $service);
		return $geovalues ? $geovalues[3] : '';
	}

	/**
	 * Geocode an address with the provided geocoding service
	 *
	 * @param unknown_type $address
	 * @param unknown_type $service
	 * @return unknown
	 */
	private static function geocode($address, $service, $mappingService) {
		global $egMapsAvailableGeoServices, $egMapsDefaultGeoService;
		
		// If the adress is already in the cache and the cache is enabled, return the coordinates
		if (self::$mEnableCache && array_key_exists($address, MapsGeocoder::$mGeocoderCache)) {
			return self::$mGeocoderCache[$address];
		}
		
		$service = self::getValidGeoService($service, $mappingService);

		// If not, use the selected geocoding service to geocode the provided adress
		switch(strtolower($service)) {
			case 'yahoo':
				self::addAutoloadClassIfNeeded('MapsYahooGeocoder', 'Maps_YahooGeocoder.php');
				$coordinates = MapsYahooGeocoder::geocode($address);
				break;
			default:
				self::addAutoloadClassIfNeeded('MapsGoogleGeocoder', 'Maps_GoogleGeocoder.php');
				$coordinates = MapsGoogleGeocoder::geocode($address);
				break;
		}

		// Add the obtained coordinates to the cache when there is a result and the cache is enabled
		if (self::$mEnableCache && isset($coordinates)) {
			MapsGeocoder::$mGeocoderCache[$address] = $coordinates;
		}

		return $coordinates;
	}

	private static function addAutoloadClassIfNeeded($className, $fileName) {
		global $wgAutoloadClasses, $egMapsIP;
		if (!array_key_exists($className, $wgAutoloadClasses)) $wgAutoloadClasses[$className] = $egMapsIP . '/Geocoders/' . $fileName;
	}
	
	/**
	 * Make sure that the geo service is one of the available
	 *
	 * @param unknown_type $service
	 * @return unknown
	 */
	private static function getValidGeoService($service, $mappingService) {
		global $egMapsAvailableGeoServices, $egMapsDefaultGeoService;
		
		if (strlen($service) < 1) {
			
			switch ($mappingService) {
				case 'googlemaps' :
					$service = 'google';
					break;
				case 'yahoomaps' :
					$service = 'yahoo';					
					break;	
				default :
					$service = $egMapsDefaultGeoService;
					break;
			}
			
		}
		else {
			if(!in_array($service, $egMapsAvailableGeoServices)) $service = $egMapsDefaultGeoService;
		}

		return $service;
	}	
}



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)

Share

About the Author

Jeroen De Dauw
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

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 1 Aug 2009
Article Copyright 2009 by Jeroen De Dauw
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid