Extension:Maps/Extending Maps

Introduction
This page goes over all steps required to add support for your own mapping service to Maps or Semantic Maps. If you have no experience on this subject, it's recommended you read this manual from top to bottom, and refer to the PHP docs for examples of how services are added and comprehending how Maps and Semantic Maps work. You can also review the actual source code of Maps and Semantic Maps, which contains sufficient comments at most places to make you understand what the code does.

Adding a new mapping service
When you want to add your own service, it's recommended you do this in your own extension, that then will hold all your code. This way the relative stability of Maps and Semantic Maps stays assured, and users can optionally add your service. The code can later on very easily be put into Maps and Semantic Maps itself.

Maps defines an array that holds the list of available mapping services and their related information in Maps.php. The name of this variable is. Adding a new service to it is as easy as creating a new array element, with as key the service name, and as value an array holding it's information.

$egMapsServices['your-service'] = array;

You need to place this code globally, and preferably in the initialization file of your extension. In other words, you can not place it in a function or method.

The next step is adding the service's information to the array. This includes general mapping service information, like aliases, but later on, you will also need to add some array items to enable specific functionality.

Aliases
Every service can have an unlimited amount of aliases. Aliases enable you to enter alternate values for the parameter specifying the mapping service, that will then be interpreted by the code as if they where the main service name. Aliases are added by placing a new array key with the name 'aliases', and an array holding the aliases as value into the service's array. The below example will add 3 aliases to your-service.

$egMapsServices['your-service'] = array(					'aliases' => array('yourservice', 'ys', 'your service')					);

Note that you need to add the 'aliases' array, even when there are no aliases.

'aliases' => array

Parameters
Maps and Semantic Maps validate the parameters used in parser functions, form inputs and queries. They do so by comparing them to an array of allowed parameter names, which also holds aliases for the names. This array is defined in MapsMapper.

private static $mainParams = array (			'service' => array,			'coordinates' => array('coords', 'location', 'locations'),			'zoom' => array,			'centre' => array('center'),			'width' => array,			'height' => array,			'controls' => array,			'label' => array,			'title' => array							);

Every service can add it's own array of service specific parameters, which will then also be allowed, when that service is used to display a map. This is done by adding a 'parameters' element to the service's array. The value of this new element should be an array which holds named elements. The names of these elements represent the main parameter name, and their value, which should be an array, contain their aliases.

$egMapsServices['your-service'] = array(					'parameters' => array( 'a-specific-parameter' => array('specific-parameter', 'specificparameter'), 'one-without-alias' => array );

Note that you need to add the 'parameters' array, even when there are no mapping service specific parameters.

Example
This example is taken from the Google Maps code in Maps, and contains both alias and parameter definitions.

$egMapsServices['googlemaps'] = array(					'aliases' => array('google', 'googlemap', 'gmap', 'gmaps'),					'parameters' => array( 'type' => array('map-type', 'map type'), 'types' => array('map-types', 'map types'), 'earth' => array, 'autozoom' => array('auto zoom', 'mouse zoom', 'mousezoom'), 'class' => array, 'style' => array )					);

Parser functions
This section describes how you can add parser function support for your new mapping service. This means that when you do the described tasks, your mapping service will be usable via Map's parser functions: display_point, display_points, display_address and display_addresses. The examples assume you have an extension that uses Exp (Extension prefix) as prefix. Please use your own prefix for your extension, and not Maps or SM.

Map class
Maps provides an abstract class MapsBaseMap that contains functionality that is common to all mapping services and enforces the implementation of several abstract methods. It is highly recommended you create a class inheriting from MapsBaseMap to handle your parser functions. This will save you a lot of work, and guarantee convention to some extend.

So, first of all create your class. It should be placed in a directory named after the mapping service, and the file name should be your extensions prefix, followed by an underscore, and then the mapping service name. The full path should then have this format:.

After this you need to do is add your class to the mapping service's info array. By doing this, Maps will recognize that your service supports the parser functions, and will also automatically load the classes into the $wgAutoloadClasses array.

$egMapsServices['your-service'] = array(					'pf' => array('class' => 'ExpYourService', 'file' => 'YourService/Exp_YourService.php', 'local' => false)					);

As you can see in the above example, the name of the array element should be 'pf', short for parser function. The value is an array containing the name of your class, the file and it's path, and a value indication if the file is local. The local parameter will cause the complete path to the maps extension directory to be added when true. If you have your own extension, the value should be set to false, and you should provide the full path in the file parameter.

The next step is adding the code to handle your mapping service to your class. First of all, start off with defining a new class, that inherits MapsBaseMap.

Now add the service name (the same one you added as array key name to $egMapsServices) to your class. This is required for Maps to be able to identify the class.

By inheriting from MapsBaseMap, you will be forced to implement 3 methods: setMapSettings, doMapServiceLoad and addSpecificMapHTML.

1. Method setMapSettings gets called before the others, and needs to set 3 class fields. This example is code from the MapsGoogleMaps class of Maps.

holds an array with default values for the mapping service specific service. In this example, the value is retrieved by a function in a special utility class. The use of such a class is discussed in the next section.

should be set to an identical prefix for your mapping service, like 'yourservice_map'. It's recommended that you make this value a setting, and put it in Maps_Settings.php, like done in the example.

should be set to the default zoom level of your mapping service. Just like with the element name prefix, it's recommended that you make a setting of this value.

2. Method doMapServiceLoad does map service specific map count and loading of dependencies. This means add an html string that contains all the stuff that needs to be loaded to $this->output. This can include loading map API's via html script tags. When no dependencies need to be loaded (which is unlikely), you still need to return an empty string. You should also set the elementNr field, which contains the number of the map. This number should also be increased. This example is again an out-take of the Google Maps code in Maps.

3. The addSpecificMapHTML should add the actual map HTML and JavaSctipt to $this->output. It also contains all mapping service specific map property manipulation. The output you add in this method is entirely dependant on your mapping service. It most likely consists out of a div element to old the map, and some javascript to create it, set it's properties and add markers. It's highly recommended that you add as little JavaScript as possible. It is not good practice to use PHP to create JavaScript. The map logic should be handled by JavaScript, and JavaScript alone. A good approach is to create a JavaScript function to create your map, put it in a seperate .js file, load it with the mapping service's dependencies, and then only add a JavaScript call to that function to $this->output.

This is the HTML output added in MapsGoogleMaps, of Maps.

Utility class
Maps uses utility classes for each mapping service that contain static functions that get called from their map class. This is done to enable other classes, like form inputs and query printers to use common code, and prevent redundancy. It is recommended you choose a similar setup, but you can also simply put all code in your map class (note that this will make extending your mapping service more difficult), or use multiple utility classes (to increase modularity when you have a lot of common code).

Geocoding services
Maps does currently not support any hook like system for geocoding services. It is however quite easy to create your own, and then submit it as a patch.

This is a very rough list of the basic steps you'll need to take to add support for a geocoding service to Maps:


 * 1) Create a new class, that inherits from MapsBaseGeocoder, and place it in the Geocoders directory of Maps.
 * 2) Add a   to your class.
 * 3) Do the call to the geocoding service there, and return an array with keys 'lat' and 'lon', containing the latitude and longitude, respectively, or false when geocoding fails.
 * 4) Modify MapsGeocoder (Maps/Maps_Geocoder.php): add a new case element to the switch statement in   with the name for your geocoding service. Call the addAutoloadClassIfNeeded method, and add a call to the geocode method in your new class.
 * 5) Add the name for your geocoding service to the $egMapsAvailableGeoServices array in Maps_Settings.php.

That are all the steps you need to do. You can have a look at the present services as examples.

Extending Semantic Maps
This section does not yet contain any helpful info. Try travelling a few days into the future to read it.

More support
If you have any additional questions, or feel some aspects of extending maps have not been documented enough, feel free to contact Jeroen De Dauw.