From: philipp Date: Mon, 10 Dec 2012 20:12:39 +0000 (+0000) Subject: Huge changes: Using OpenLayers with Google Maps v3 instead of direct Google Maps v2. X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/mediawiki_extensions/wrmap.git/commitdiff_plain/1b96d2f3682d951792de2f8be8d23e1a4bd1d58b?hp=142dc33879cdbb83d4c2d0de5164853d00c78d48 Huge changes: Using OpenLayers with Google Maps v3 instead of direct Google Maps v2. git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/extensions/wrmap@1225 7aebc617-e5e2-0310-91dc-80fb5f6d2477 --- diff --git a/wrmap.body.php b/wrmap.body.php index 95e88ae..e71b1ea 100644 --- a/wrmap.body.php +++ b/wrmap.body.php @@ -1,143 +1,44 @@ setHook('wrgmap', array($wrGoogleMaps, 'render')); - return true; -} - - - -// Java script -// ----------- - -// Global JavaScript functions -define('WRGMAPJSFUNCTIONS', << -//" + name+ "");}); - return marker; -} -//]]> - -JAVASCRIPT -); - - -/** This class was inpired by the GoogleMaps class of the GoogleMaps extension. */ -class WrGoogleMaps { - /// the Google API key (obtained from - /// http://www.google.com/apis/maps/signup.html) - private $apiKey = null; - - /// How many tags are on the current page? - private $mapsCount = 0; - - /// Constructor - function WrGoogleMaps($apiKey) { - $this->apiKey = $apiKey; - } - +class WrMap { /// Renders the tag /// @param $content string - the content of the tag /// @param $args array - the array of attribute name/value pairs for the tag /// @param $parser Parser - the MW Parser object for the current page /// /// @return string - the html for rendering the map - function render($content, $args, $parser) { - ++$this->mapsCount; - - // Decode data + public static function render($content, $args, $parser, $frame) { + // Get center and zoom level from $args if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648; // latitude as float value if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655; // longitude as float value if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level - $staticSizeX = 600; - $staticSizeY = 450; $latitude_s = sprintf('%.6F', $latitude); $longitude_s = sprintf('%.6F', $longitude); // Query database $dbr = wfGetDB(DB_SLAVE); $res = $dbr->select('wrsledruncache', array('page_title', 'position_latitude', 'position_longitude'), array('show_in_overview', 'not under_construction')); - $sleddingRoutes = array(); - while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute; + $sledruns = array(); + while ($sledrun = $dbr->fetchRow($res)) $sledruns[] = $sledrun; $dbr->freeResult($res); - // Load Google Maps Script and define functions - $output = ''; - if ($this->mapsCount == 1) { - $output .= '' . "\n"; - $output .= WRGMAPJSFUNCTIONS; - } - - // Create static map image link - $staticLink = "http://maps.google.com/staticmap?center=$latitude_s,$longitude_s&zoom=$zoom&size=${staticSizeX}x$staticSizeY&key=$this->apiKey"; - $staticMarkers = array(); - foreach ($sleddingRoutes as $s) { - $lat = $s['position_latitude']; // this is a string - $lon = $s['position_longitude']; // this is a string - $staticMarkers[] = sprintf('%s,%s,bluer', $lat, $lon); - } - if (count($staticMarkers) > 0) $staticLink .= '&markers=' . implode('|', $staticMarkers); - - // Create
element where the map is placed in - $mapName = 'wrgmap' . $this->mapsCount; - $output .= ''; + $parserOutput = $parser->getOutput(); + $parserOutput->addHeadItem('', 'googlemaps'); + $parserOutput->addModules('ext.wrmap'); - // Return output - $output .= '\n"; - $output .= '\n"; - - return wrCommonReplaceByMarker($output, 'wrmap'); - } - - // returns a string that creates a map object called 'map' - // $latitude, $longitude and $zoom have to be float/integer values. - private function addMap($mapName, $latitude, $longitude, $zoom) { - return "\tvar map = new GMap2(document.getElementById('$mapName'), {'mapTypes': [G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP]});\n" . - "\tmap.addControl(new GLargeMapControl());\n" . - "\tmap.addControl(new GMapTypeControl());\n" . - "\tmap.addControl(new GScaleControl());\n" . - sprintf("\tmap.setCenter(new GLatLng(%.6F, %.6F), %d);\n", $latitude, $longitude, $zoom) . - "\tmap.setMapType(G_PHYSICAL_MAP);\n"; - // "\tmap.enableScrollWheelZoom();\n"; + $output .= "
\n"; + + return $output; } - // returns a string with a add marker javascript call - // $latitude and $longitude are expected as string - private function addJsMarker($latitude, $longitude, $pageTitle) { - return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n"; - } - } diff --git a/wrmap.js b/wrmap.js new file mode 100644 index 0000000..83daff9 --- /dev/null +++ b/wrmap.js @@ -0,0 +1,82 @@ +function init_wrmap(i, jq_map) { + jq_map = $(jq_map); + var jq_sledruns = jq_map.children(); + jq_sledruns.detach(); + + // Introduce EPSG:3857 as an alias of the built in EPSG:900913 projection (both are the "Google/OSM" projections) + var EPSG4326 = new OpenLayers.Projection("EPSG:4326"); // lon/lat + var EPSG3857 = new OpenLayers.Projection("EPSG:3857"); // google + + // Create the map + var map = new OpenLayers.Map(jq_map.context, { + projection: EPSG3857, + displayProjection: EPSG4326, + units: "m", + theme: null + }); + + // Google Layer + var layer_map = new OpenLayers.Layer.Google("Google Physical", { + type: google.maps.MapTypeId.TERRAIN + }); + + // Alternative: OSM map + // var layer_map = new OpenLayers.Layer.OSM({ + // maxExtent: new OpenLayers.Bounds(1050000, 5880000, 1850000, 6090000)}); + + // Alternative: Microsoft Bing Maps + // var layer_map = new OpenLayers.Layer.Bing({ + // type: "Road", + // key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA"}); + + // Sledrun layer + var layer_sledruns = new OpenLayers.Layer.Vector("Rodelbahnen", { + styleMap: new OpenLayers.StyleMap({ + "default": new OpenLayers.Style({ + externalGraphic: "/vorlagen/gmap_rodelbahn_c.png", + graphicWidth: 17, + graphicHeight: 17, + graphicXOffset: 0, + graphicYOffset: 0, + graphicZIndex: 11, + backgroundGraphic: "/vorlagen/gmap_rodelbahn_c_s.png", + backgroundWidth: 23, + backgroundHeight: 23, + backgroundXOffset: 0, + backgroundYOffset: 0, + backgroundZIndex: 12, + title: "${label}" + }), + "highlight": new OpenLayers.Style({ + label: "${label}" + }) + + }), + rendererOptions: {yOrdering: true} + }); + jq_sledruns.each(function(j, jq_sledrun) { + jq_sledrun = $(jq_sledrun); + var lon = parseFloat(jq_sledrun.attr('data-lon')); + var lat = parseFloat(jq_sledrun.attr('data-lat')); + var point = new OpenLayers.Geometry.Point(lon, lat).transform(EPSG4326, EPSG3857); + layer_sledruns.addFeatures([new OpenLayers.Feature.Vector(point, {label: jq_sledrun.attr('data-title')})]); + }); + + // Center map + map.addLayers([layer_map, layer_sledruns]); + var lon = parseFloat(jq_map.attr('data-center-lon')); + var lat = parseFloat(jq_map.attr('data-center-lat')); + var zoom = parseInt(jq_map.attr('data-zoom')); + map.setCenter(new OpenLayers.LonLat(lon, lat).transform(EPSG4326, map.getProjectionObject()), zoom); + +} + + +function init_wrmaps() { + var jq_maps = $('.wrmap'); // all wrmap
elements + jq_maps.each(init_wrmap); +} + + +$(document).ready(init_wrmaps); + diff --git a/wrmap.php b/wrmap.php index 2240e77..2e9924b 100644 --- a/wrmap.php +++ b/wrmap.php @@ -1,21 +1,50 @@ 'Winterrodeln Map', - 'version' => '1.4.0', + 'version' => '2.0.0', 'author' =>'Philipp Spitzer', 'url' => 'http://www.winterrodeln.org/trac/wiki/WrMap', - 'description' => 'This extension creates a map with GoogleMaps to show sledrun routes.' + 'description' => 'This extension creates a map using OpenLayers to show sledruns.' ); + +$wgResourceModules['ext.wrmap'] = array( + 'scripts' => array('openlayers/OpenLayers.js', 'wrmap.js'), + 'styles' => 'openlayers/theme/default/style.css', + + // When your module is loaded, these messages will be available through mw.msg() + //'messages' => array( 'myextension-hello-world', 'myextension-goodbye-world' ), + + // If your scripts need code from other modules, list their identifiers as dependencies + // and ResourceLoader will make sure they're loaded before you. + // You don't need to manually list 'mediawiki' or 'jquery', which are always loaded. + //'dependencies' => array( 'jquery.ui.datepicker' ), + + // You need to declare the base path of the file paths in 'scripts' and 'styles' + 'localBasePath' => dirname( __FILE__ ), + + // ... and the base from the browser as well. For extensions this is made easy, + // you can use the 'remoteExtPath' property to declare it relative to where the wiki + // has $wgExtensionAssetsPath configured: + 'remoteExtPath' => 'wrmap', + + 'position' => 'top' +); + + +$wgAutoloadClasses['WrMap'] = dirname(__FILE__) . '/wrmap.body.php'; + + $wgHooks['ParserFirstCallInit'][] = 'wrMapParserFirstCallInit'; -$wgHooks['ParserAfterTidy'][] = 'wrCommonMarkerAfterTidy'; -require_once dirname(__FILE__) . '/wrmap.body.php'; +function wrMapParserFirstCallInit($parser) { + $parser->setHook('wrgmap', 'WrMap::render'); + return true; +} + ?>