Huge changes: Using OpenLayers with Google Maps v3 instead of direct Google Maps v2.
[philipp/winterrodeln/mediawiki_extensions/wrmap.git] / wrmap.body.php
index 95e88aed485ea56653df2abfe7f7c2b6f5646a8b..e71b1eacaf81d8ba34c97565e0d714546339733c 100644 (file)
 <?php
 
-function wrMapParserFirstCallInit() {
-       global $wgParser;
-       global $wgGoogleMapsKey;
-       $wrGoogleMaps = new WrGoogleMaps($wgGoogleMapsKey);
-       $wgParser->setHook('wrgmap', array($wrGoogleMaps, 'render'));
-       return true;
-}
-
-
-
-// Java script
-// -----------
-
-// Global JavaScript functions
-define('WRGMAPJSFUNCTIONS', <<<JAVASCRIPT
-<script type="text/javascript">
-//<![CDATA[
-var wrSleddingIcon = new GIcon(G_DEFAULT_ICON);
-wrSleddingIcon.image = "/vorlagen/gmap_rodelbahn_c.png";
-wrSleddingIcon.shadow = "/vorlagen/gmap_rodelbahn_c_s.png";
-wrSleddingIcon.iconSize = new GSize(17, 17);
-wrSleddingIcon.shadowSize = new GSize(23, 23);
-wrSleddingIcon.iconAnchor = new GPoint(9, 9);
-wrSleddingIcon.infoWindowAnchor = new GPoint(9, 9);
-
-
-function wrCreateMarker(latitude, longitude, name, icon) {
-       var point = new GLatLng(latitude, longitude);
-       var marker = new GMarker(point, icon);
-       var articlePath = "$wgArticlePath";
-       var p = articlePath.replace("\$1", name.replace(' ', '_'));
-       GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml("<strong><a href='" + p + "'>" + name+ "</a></strong>");});
-       return marker;
-}
-//]]>
-</script>
-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 <wrgmap> tags are on the current page?
-       private $mapsCount = 0;
-       
-       /// Constructor
-       function WrGoogleMaps($apiKey) {
-               $this->apiKey = $apiKey;
-       }
-       
+class WrMap {
        /// Renders the <wrgmap> tag
        /// @param $content string - the content of the <wrgmap> 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 .= '<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\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 <div/> element where the map is placed in
-               $mapName = 'wrgmap' . $this->mapsCount;
-               $output .= '<div id="' . $mapName . '" style="width: 100%; height: 450px; border-style:none; display:none;"></div>';
+               $parserOutput = $parser->getOutput();
+               $parserOutput->addHeadItem('<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.8&amp;sensor=false"></script>', 'googlemaps');
+               $parserOutput->addModules('ext.wrmap');
                
-               // Return output
-               $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
-               $output .= 'if (GBrowserIsCompatible()) {' . "\n";
-               $output .= "\tdocument.getElementById(\"$mapName\").style.display = \"block\";\n";
-               $output .= $this->addMap($mapName, $latitude, $longitude, $zoom);
-               foreach ($sleddingRoutes as $s) {
-                       $lat = $s['position_latitude'];
-                       $lon = $s['position_longitude'];
-                       $pageTitle = $s['page_title'];
+               // Create <div/> element where the map is placed in
+               $output = "<div class=\"wrmap\" style=\"width: 100%; height: 450px; border-style:none;\" data-center-lon=\"$longitude_s\" data-center-lat=\"$latitude_s\" data-zoom=\"$zoom\">\n";
+               foreach ($sledruns as $sledrun) {
+                       $lat = $sledrun['position_latitude'];
+                       $lon = $sledrun['position_longitude'];
                        if (!$lat || !$lon) continue;
-                       $output .= $this->addJsMarker($lat, $lon, $pageTitle);
+                       $output .= "<p data-lon=\"$lon\" data-lat=\"$lat\" data-title=\"{$sledrun['page_title']}\" />\n";
                }
-               $output .= "} else {\n"; // browser not compatible -> create static map
-               $output .= "\tdocument.write(\"<img alt=\\\"Landkarte mit Rodelbahnen\\\" src=\\\"" . htmlspecialchars($staticLink) . "\\\" width=\\\"$staticSizeX\\\" height=\\\"$staticSizeY\\\" />\");\n";
-               $output .= "}\n//]]>\n</script>\n";
-               $output .= '<noscript><img alt="Landkarte mit Rodelbahnen" src="' . htmlspecialchars($staticLink) . "\" width=\"$staticSizeX\" height=\"$staticSizeY\" /></noscript>\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 .= "</div>\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";
-       }
-       
 }