http://www.mediawiki.org/wiki/Special:Code/MediaWiki/58452
Revision: 58452 Author: jeroendedauw Date: 2009-11-03 00:34:24 +0000 (Tue, 03 Nov 2009) Log Message: ----------- Changes for 0.4 (see: http://www.mediawiki.org/wiki/Extension:Maps/Future) Modified Paths: -------------- trunk/extensions/Maps/MapUtilityFunctions.js trunk/extensions/Maps/Maps.php trunk/extensions/Maps/Maps_MapFeature.php trunk/extensions/Maps/Maps_Mapper.php trunk/extensions/Maps/Maps_Settings.php trunk/extensions/Maps/Maps_Utils.php trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispMap.php trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispPoint.php trunk/extensions/Maps/OpenLayers/OpenLayerFunctions.js trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispMap.php trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispPoint.php trunk/extensions/Maps/OpenStreetMaps/Maps_OSMUtils.php trunk/extensions/Maps/OpenStreetMaps/OSMFunctions.js Modified: trunk/extensions/Maps/MapUtilityFunctions.js =================================================================== --- trunk/extensions/Maps/MapUtilityFunctions.js 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/MapUtilityFunctions.js 2009-11-03 00:34:24 UTC (rev 58452) @@ -9,26 +9,11 @@ * @author Jeroen De Dauw */ -/* -function addLoadEvent(func) { - var oldonload = window.onload; - if (typeof oldonload == 'function') { - window.onload = function() { - oldonload(); - func(); - }; - } - else { - window.onload = func; - } -} -*/ - function convertLatToDMS (val) { - return Math.abs(val) + "° " + ( val < 0 ? "S" : "N" ); + return Math.abs(val) + "\xB0 " + ( val < 0 ? "S" : "N" ); } function convertLngToDMS (val) { - return Math.abs(val) + "° " + ( val < 0 ? "W" : "E" ); + return Math.abs(val) + "\xB0 " + ( val < 0 ? "W" : "E" ); } \ No newline at end of file Modified: trunk/extensions/Maps/Maps.php =================================================================== --- trunk/extensions/Maps/Maps.php 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/Maps.php 2009-11-03 00:34:24 UTC (rev 58452) @@ -23,7 +23,7 @@ die( 'Not an entry point.' ); } -define('Maps_VERSION', '0.4 a9'); +define('Maps_VERSION', '0.4 a10'); $egMapsScriptPath = $wgScriptPath . '/extensions/Maps'; $egMapsIP = $IP . '/extensions/Maps'; Modified: trunk/extensions/Maps/Maps_MapFeature.php =================================================================== --- trunk/extensions/Maps/Maps_MapFeature.php 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/Maps_MapFeature.php 2009-11-03 00:34:24 UTC (rev 58452) @@ -17,19 +17,16 @@ /** * Set the map service specific element name and the javascript function handling the displaying of an address - * */ protected abstract function setMapSettings(); /** * Map service specific map count and loading of dependencies - * */ protected abstract function doMapServiceLoad(); /** * Adds the HTML specific to the mapping service to the output - * */ protected abstract function addSpecificMapHTML(); @@ -51,7 +48,6 @@ /** * Sets the map properties as class fields. - * */ protected function manageMapProperties($mapProperties, $className) { global $egMapsServices; @@ -67,7 +63,6 @@ } } - // TODO: refactor down - not every mapping feature/service should support a controls parameter MapsMapper::enforceArrayValues($this->controls); MapsUtils::makeMapSizeValid($this->width, $this->height); @@ -75,7 +70,6 @@ /** * Sets the $mapName field, using the $elementNamePrefix and $elementNr. - * */ protected function setMapName() { $this->mapName = $this->elementNamePrefix.'_'.$this->elementNr; Modified: trunk/extensions/Maps/Maps_Mapper.php =================================================================== --- trunk/extensions/Maps/Maps_Mapper.php 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/Maps_Mapper.php 2009-11-03 00:34:24 UTC (rev 58452) @@ -1,6 +1,6 @@ <?php -/** +/** * A class that holds static helper functions for common functionality that is not map-spesific. * * @file Maps_Mapper.php Modified: trunk/extensions/Maps/Maps_Settings.php =================================================================== --- trunk/extensions/Maps/Maps_Settings.php 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/Maps_Settings.php 2009-11-03 00:34:24 UTC (rev 58452) @@ -64,11 +64,11 @@ include_once $egMapsIP . '/GoogleMaps/Maps_GoogleMaps.php'; // Google Maps include_once $egMapsIP . '/OpenLayers/Maps_OpenLayers.php'; // OpenLayers include_once $egMapsIP . '/YahooMaps/Maps_YahooMaps.php'; // Yahoo! Maps -//include_once $egMapsIP . '/OpenStreetMaps/Maps_OSM.php'; // OpenLayers optimized for OSM +include_once $egMapsIP . '/OpenStreetMaps/Maps_OSM.php'; // OpenLayers optimized for OSM # Array of String. Array containing all the mapping services that will be made available to the user. # Currently Maps provides the following services: googlemaps, yahoomaps, openlayers -$egMapsAvailableServices = array('googlemaps', 'yahoomaps', 'openlayers'/*, 'osm'*/); +$egMapsAvailableServices = array('googlemaps', 'yahoomaps', 'openlayers', 'osm'); # String. The default mapping service, which will be used when no default service is prsent in the # $egMapsDefaultServices array for a certain feature. A service that supports all features is recommended. @@ -203,9 +203,8 @@ $egMapsOpenLayersZoom = 13; # Array of String. The default controls for Open Layers. This value will only be used when the user does not provide one. -# Available values: layerswitcher, mouseposition, panzoom, panzoombar, scaleline, navigation, keyboarddefaults, overviewmap, permalink -# Note: panzoom and panzoombar can NOT be used together -$egMapsOLControls = array('layerswitcher', 'mouseposition', 'panzoombar', 'scaleline', 'navigation'); +# Available values: layerswitcher, mouseposition, autopanzoom, panzoom, panzoombar, scaleline, navigation, keyboarddefaults, overviewmap, permalink +$egMapsOLControls = array('layerswitcher', 'mouseposition', 'autopanzoom', 'scaleline', 'navigation'); # Array of String. The default layers for Open Layers. This value will only be used when the user does not provide one. # Available values: google, bing, yahoo, openlayers, nasa @@ -220,3 +219,7 @@ # Integer. The default zoom of a map. This value will only be used when the user does not provide one. $egMapsOSMZoom = 13; + +# Array of String. The default controls for OSM maps. This value will only be used when the user does not provide one. +# Available values: layerswitcher, mouseposition, autopanzoom, panzoom, panzoombar, scaleline, navigation, keyboarddefaults, overviewmap, permalink +$egMapsOSMControls = array('layerswitcher', 'mouseposition', 'autopanzoom', 'scaleline', 'navigation'); \ No newline at end of file Modified: trunk/extensions/Maps/Maps_Utils.php =================================================================== --- trunk/extensions/Maps/Maps_Utils.php 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/Maps_Utils.php 2009-11-03 00:34:24 UTC (rev 58452) @@ -126,7 +126,7 @@ */ public static function makeMapSizeValid(&$width, &$height) { global $egMapsSizeRestrictions; - + if ($width < $egMapsSizeRestrictions['width'][0]) { $width = $egMapsSizeRestrictions['width'][0]; } @@ -134,11 +134,11 @@ $width = $egMapsSizeRestrictions['width'][1]; } - if ($width < $egMapsSizeRestrictions['height'][0]) { - $width = $egMapsSizeRestrictions['height'][0]; + if ($height < $egMapsSizeRestrictions['height'][0]) { + $height = $egMapsSizeRestrictions['height'][0]; } - else if($width > $egMapsSizeRestrictions['height'][1]) { - $width = $egMapsSizeRestrictions['height'][1]; + else if($height > $egMapsSizeRestrictions['height'][1]) { + $height = $egMapsSizeRestrictions['height'][1]; } } Modified: trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispMap.php =================================================================== --- trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispMap.php 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispMap.php 2009-11-03 00:34:24 UTC (rev 58452) @@ -58,7 +58,7 @@ $this->output .= "<div id='$this->mapName' style='width: {$this->width}px; height: {$this->height}px; background-color: #cccccc;'></div> <script type='$wgJsMimeType'> /*<![CDATA[*/ addOnloadHook( - initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[]) + initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[], $this->height) ); /*]]>*/ </script>"; } Modified: trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispPoint.php =================================================================== --- trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispPoint.php 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispPoint.php 2009-11-03 00:34:24 UTC (rev 58452) @@ -66,8 +66,8 @@ $label = array_key_exists('label', $markerData) ? $markerData['label'] : $this->label; $title = str_replace("'", "\'", $title); - $label = str_replace("'", "\'", $label); - + $label = str_replace("'", "\'", $label); + $icon = array_key_exists('icon', $markerData) ? $markerData['icon'] : ''; $markerItems[] = "getOLMarkerData($lon, $lat, '$title', '$label', '$icon')"; } @@ -77,7 +77,7 @@ $this->output .= "<div id='$this->mapName' style='width: {$this->width}px; height: {$this->height}px; background-color: #cccccc;'></div> <script type='$wgJsMimeType'> /*<![CDATA[*/ addOnloadHook( - initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[$markersString]) + initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[$markersString], $this->height) ); /*]]>*/ </script>"; } Modified: trunk/extensions/Maps/OpenLayers/OpenLayerFunctions.js =================================================================== --- trunk/extensions/Maps/OpenLayers/OpenLayerFunctions.js 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/OpenLayers/OpenLayerFunctions.js 2009-11-03 00:34:24 UTC (rev 58452) @@ -11,7 +11,7 @@ * Creates and initializes an OpenLayers map. * The resulting map is returned by the function but no further handling is required in most cases. */ -function initOpenLayer(mapName, lon, lat, zoom, mapTypes, controls, marker_data){ +function initOpenLayer(mapName, lon, lat, zoom, mapTypes, controls, marker_data, height){ // Create a new OpenLayers map with without any controls on it var mapOptions = { @@ -31,6 +31,10 @@ // If a string is provided, find the correct name for the control, and use eval to create the object itself if (typeof controls[i] == 'string') { + if (controls[i].toLowerCase() == 'autopanzoom') { + if (height > 140) controls[i] = height > 320 ? 'panzoombar' : 'panzoom'; + } + control = getValidControlName(controls[i]); if (control) { Modified: trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispMap.php =================================================================== --- trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispMap.php 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispMap.php 2009-11-03 00:34:24 UTC (rev 58452) @@ -50,19 +50,22 @@ public function addSpecificMapHTML() { global $wgJsMimeType; + $controlItems = MapsOSMUtils::createControlsString($this->controls); + $this->output .= <<<EOT <script type='$wgJsMimeType'>slippymaps['$this->mapName'] = new slippymap_map('$this->mapName', { mode: 'osm-wm', layer: 'osm-like', - locale: 'en', + locale: 'en', lat: $this->centre_lat, lon: $this->centre_lon, zoom: $this->zoom, width: $this->width, height: $this->height, - marker: 0 + markers: [], + controls: [$controlItems] });</script> - + <!-- map div --> <div id='$this->mapName' class='map' style='width:{$this->width}px; height:{$this->height}px;'> <script type='$wgJsMimeType'>slippymaps['$this->mapName'].init();</script> Modified: trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispPoint.php =================================================================== --- trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispPoint.php 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/OpenStreetMaps/Maps_OSMDispPoint.php 2009-11-03 00:34:24 UTC (rev 58452) @@ -64,17 +64,34 @@ $label = str_replace("'", "\'", $label); $icon = array_key_exists('icon', $markerData) ? $markerData['icon'] : ''; - $markerItems[] = "getOLMarkerData($lon, $lat, '$title', '$label', '$icon')"; + $markerItems[] = "getOSMMarkerData($lon, $lat, '$title', '$label', '$icon')"; } $markersString = implode(',', $markerItems); - $this->output .= "<div id='$this->mapName' style='width: {$this->width}px; height: {$this->height}px; background-color: #cccccc;'></div> - <script type='$wgJsMimeType'> /*<![CDATA[*/ - addOnloadHook( - initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[$markersString]) - ); - /*]]>*/ </script>"; + $controlItems = MapsOSMUtils::createControlsString($this->controls); + + $this->output .= <<<EOT + <script type='$wgJsMimeType'>slippymaps['$this->mapName'] = new slippymap_map('$this->mapName', { + mode: 'osm-wm', + layer: 'osm-like', + locale: 'en', + lat: $this->centre_lat, + lon: $this->centre_lon, + zoom: $this->zoom, + width: $this->width, + height: $this->height, + markers: [$markersString], + controls: [$controlItems] + + });</script> + + <!-- map div --> + <div id='$this->mapName' class='map' style='width:{$this->width}px; height:{$this->height}px;'> + <script type='$wgJsMimeType'>slippymaps['$this->mapName'].init();</script> + <!-- /map div --> + </div> +EOT; } } \ No newline at end of file Modified: trunk/extensions/Maps/OpenStreetMaps/Maps_OSMUtils.php =================================================================== --- trunk/extensions/Maps/OpenStreetMaps/Maps_OSMUtils.php 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/OpenStreetMaps/Maps_OSMUtils.php 2009-11-03 00:34:24 UTC (rev 58452) @@ -80,13 +80,11 @@ public static function getDefaultParams() { return array ( - 'layers' => array(), - 'baselayer' => '' ); } /** - * If this is the first open layers map on the page, load the API, styles and extra JS functions + * If this is the first OSM map on the page, load the OpenLayers API, OSM styles and extra JS functions * * @param string $output */ @@ -102,6 +100,17 @@ <script type='$wgJsMimeType' src='$egMapsScriptPath/OpenStreetMaps/OSMFunctions.js?$wgStyleVersion'></script> <script type='$wgJsMimeType'>slippymaps = Array();</script>\n"; } + } + + /** + * Build up a csv string with the controls, to be outputted as a JS array + * + * @param array $controls + * @return csv string + */ + public static function createControlsString(array $controls) { + global $egMapsOSMControls; + return MapsMapper::createJSItemsString($controls, $egMapsOSMControls); } } Modified: trunk/extensions/Maps/OpenStreetMaps/OSMFunctions.js =================================================================== --- trunk/extensions/Maps/OpenStreetMaps/OSMFunctions.js 2009-11-03 00:12:12 UTC (rev 58451) +++ trunk/extensions/Maps/OpenStreetMaps/OSMFunctions.js 2009-11-03 00:34:24 UTC (rev 58452) @@ -52,7 +52,30 @@ slippymaps[keyName].init(); } } + +/** + * Gets a valid control name (with excat lower and upper case letters), + * or returns false when the control is not allowed. + */ +function getValidControlName(control) { + var OLControls = ['ArgParser', 'Attribution', 'Button', 'DragFeature', 'DragPan', + 'DrawFeature', 'EditingToolbar', 'GetFeature', 'KeyboardDefaults', 'LayerSwitcher', + 'Measure', 'ModifyFeature', 'MouseDefaults', 'MousePosition', 'MouseToolbar', + 'Navigation', 'NavigationHistory', 'NavToolbar', 'OverviewMap', 'Pan', + 'Panel', 'PanPanel', 'PanZoom', 'PanZoomBar', 'Permalink', + 'Scale', 'ScaleLine', 'SelectFeature', 'Snapping', 'Split', + 'WMSGetFeatureInfo', 'ZoomBox', 'ZoomIn', 'ZoomOut', 'ZoomPanel', + 'ZoomToMaxExtent']; + for (i in OLControls) { + if (control == OLControls[i].toLowerCase()) { + return OLControls[i]; + } + } + + return false; +} + function slippymap_map(mapId, mapParams) { var self = this; this.mapId = mapId; @@ -75,17 +98,29 @@ ]); */ + /* this.mapOptions = { controls: [ new OpenLayers.Control.Navigation(), new OpenLayers.Control.ArgParser(), new OpenLayers.Control.Attribution(), - /* buttonsPanel */ ] + // buttonsPanel + ] }; + */ - /* Add the zoom bar control, except if the map is only little */ - if (this.height > 320) - this.mapOptions.controls.push(new OpenLayers.Control.PanZoomBar()); - else if (this.height > 140) - this.mapOptions.controls.push(new OpenLayers.Control.PanZoom()); + // Add the controls + this.mapOptions = {controls: []}; + + for (i in this.controls) { + if (this.controls[i].toLowerCase() == 'autopanzoom') { + if (this.height > 140) this.controls[i] = this.height > 320 ? 'panzoombar' : 'panzoom'; + } + + control = getValidControlName(this.controls[i]); + + if (control) { + eval(' this.mapOptions.controls.push( new OpenLayers.Control.' + control + '() ); '); + } + } } slippymap_map.prototype.init = function() { @@ -96,12 +131,36 @@ this.map = this.osm_create(this.mapId, this.lon, this.lat, this.zoom); - if (this.marker) { - var markers = new OpenLayers.Layer.Markers( "Markers" ); - this.map.addLayer(markers); - var icon = OpenLayers.Marker.defaultIcon(); - markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(this.lon, this.lat).transform(new OpenLayers.Projection('EPSG:4326'), this.map.getProjectionObject()), icon)); + var centerIsSet = this.lon != null && this.lat != null; + + var bounds = null; + + if (this.markers.length > 0) { + var markerLayer = new OpenLayers.Layer.Markers('Markers'); + markerLayer.id= 'markerLayer'; + this.map.addLayer(markerLayer); + + if (this.markers.length > 1 && (!centerIsSet || this.zoom == null)) { + bounds = new OpenLayers.Bounds(); + } + + for (i in this.markers) { + this.markers[i].lonlat.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); + if (bounds != null) bounds.extend(this.markers[i].lonlat); // Extend the bounds when no center is set + markerLayer.addMarker(getOSMMarker(markerLayer, this.markers[i], this.map.getProjectionObject())); // Create and add the marker + } + } + + if (bounds != null) map.zoomToExtent(bounds); // If a bounds object has been created, use it to set the zoom and center + + if (centerIsSet) { // When the center is provided, set it + var centre = new OpenLayers.LonLat(this.lon, this.lat); + centre.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")); + this.map.setCenter(centre); + } + + if (this.zoom != null) this.map.zoomTo(this.zoom); // When the zoom is provided, set it } slippymap_map.prototype.osm_create = function(mapId, lon, lat, zoom) { @@ -109,7 +168,7 @@ var map = new OpenLayers.Map(mapId, this.mapOptions /* all provided for by OSM.js */); if (this.layer == 'osm-like') { - osmLayer = new OpenLayers.Layer.OSM("meh", 'http://cassini.toolserver.org/tiles/osm-like/' + this.locale + '/${z}/${x}/${y}.png'); + osmLayer = new OpenLayers.Layer.OSM("OpenStreetMaps", 'http://cassini.toolserver.org/tiles/osm-like/' + this.locale + '/${z}/${x}/${y}.png'); } map.addLayers([osmLayer]); @@ -132,5 +191,47 @@ ); } +function getOSMMarkerData(lon, lat, title, label, icon) { + lonLat = new OpenLayers.LonLat(lon, lat); + return { + lonlat: lonLat, + title: title, + label: label, + icon: icon + }; +} +function getOSMMarker(markerLayer, markerData, projectionObject) { + var marker; + + if (markerData.icon != '') { + marker = new OpenLayers.Marker(markerData.lonlat, new OpenLayers.Icon(markerData.icon)); + } else { + marker = new OpenLayers.Marker(markerData.lonlat); + } + + if (markerData.title.length + markerData.label.length > 0 ) { + + // This is the handler for the mousedown event on the marker, and displays the popup + marker.events.register('mousedown', marker, + function(evt) { + var popup = new OpenLayers.Feature(markerLayer, markerData.lonlat).createPopup(true); + + if (markerData.title.length > 0 && markerData.label.length > 0) { // Add the title and label to the popup text + popup.setContentHTML('<b>' + markerData.title + "</b><hr />" + markerData.label); + } + else { + popup.setContentHTML(markerData.title + markerData.label); + } + + popup.setOpacity(0.85); + markerLayer.map.addPopup(popup); + OpenLayers.Event.stop(evt); // Stop the event + } + ); + + } + + return marker; +} _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs