3 function wrMapParserFirstCallInit() {
5 global $wgGoogleMapsKey;
6 $wrGoogleMaps = new WrGoogleMaps($wgGoogleMapsKey);
7 $wgParser->setHook('wrgmap', array($wrGoogleMaps, 'render'));
16 // Global JavaScript functions
17 define(WRGMAPJSFUNCTIONS, <<<JAVASCRIPT
18 <script type="text/javascript">
20 var wrSleddingIcon = new GIcon(G_DEFAULT_ICON);
21 wrSleddingIcon.image = "/vorlagen/gmap_rodelbahn_c.png";
22 wrSleddingIcon.shadow = "/vorlagen/gmap_rodelbahn_c_s.png";
23 wrSleddingIcon.iconSize = new GSize(17, 17);
24 wrSleddingIcon.shadowSize = new GSize(23, 23);
25 wrSleddingIcon.iconAnchor = new GPoint(9, 9);
26 wrSleddingIcon.infoWindowAnchor = new GPoint(9, 9);
29 function wrCreateMarker(latitude, longitude, name, icon) {
30 var point = new GLatLng(latitude, longitude);
31 var marker = new GMarker(point, icon);
32 var articlePath = "$wgArticlePath";
33 var p = articlePath.replace("\$1", name.replace(' ', '_'));
34 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml("<strong><a href='" + p + "'>" + name+ "</a></strong>");});
43 /** This class was inpired by the GoogleMaps class of the GoogleMaps extension. */
45 /// the Google API key (obtained from
46 /// http://www.google.com/apis/maps/signup.html)
47 private $apiKey = null;
49 /// How many <wrgmap> tags are on the current page?
50 private $mapsCount = 0;
53 function WrGoogleMaps($apiKey) {
54 $this->apiKey = $apiKey;
57 /// Renders the <wrgmap> tag
58 /// @param $content string - the content of the <wrgmap> tag
59 /// @param $args array - the array of attribute name/value pairs for the tag
60 /// @param $parser Parser - the MW Parser object for the current page
62 /// @return string - the html for rendering the map
63 function render($content, $args, &$parser) {
67 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648;
68 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655;
69 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
74 $dbr = wfGetDB(DB_SLAVE);
75 $res = $dbr->select('wrsleddingcache', array('page_title', 'position_latitude', 'position_longitude'), array('show_in_overview', 'not under_construction'));
76 $sleddingRoutes = array();
77 while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute;
78 $dbr->freeResult($res);
80 // Load Google Maps Script and define functions
82 if ($this->mapsCount == 1) {
83 $output .= '<script src="http://maps.google.com/maps?file=api&v=2&key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\n";
84 $output .= WRGMAPJSFUNCTIONS;
87 // Create static map image link
88 $staticLink = "http://maps.google.com/staticmap?center=$latitude,$longitude&zoom=$zoom&size=${staticSizeX}x$staticSizeY&key=$this->apiKey";
89 $staticMarkers = array();
90 foreach ($sleddingRoutes as $s) {
91 $lat = $s['position_latitude'];
92 $lon = $s['position_longitude'];
93 $staticMarkers[] = sprintf('%.3f,%.3f,bluer', $lat, $lon);
95 if (count($staticMarkers) > 0) $staticLink .= '&markers=' . implode('|', $staticMarkers);
97 // Create <div/> element where the map is placed in
98 $mapName = 'wrgmap' . $this->mapsCount;
99 $output .= '<div id="' . $mapName . '" style="width: 100%; height: 450px; border-style:none; display:none;"></div>';
102 $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
103 $output .= 'if (GBrowserIsCompatible()) {' . "\n";
104 $output .= "\tdocument.getElementById(\"$mapName\").style.display = \"block\";\n";
105 $output .= $this->addMap($mapName, $latitude, $longitude, $zoom);
106 foreach ($sleddingRoutes as $s) {
107 $lat = $s['position_latitude'];
108 $lon = $s['position_longitude'];
109 $pageTitle = $s['page_title'];
110 if (!$lat || !$lon) continue;
111 $output .= $this->addJsMarker($lat, $lon, $pageTitle);
113 $output .= "} else {\n"; // browser not compatible -> create static map
114 $output .= "\tdocument.write(\"<img alt=\\\"Landkarte mit Rodelbahnen\\\" src=\\\"" . htmlspecialchars($staticLink) . "\\\" width=\\\"$staticSizeX\\\" height=\\\"$staticSizeY\\\" />\");\n";
115 $output .= "}\n//]]>\n</script>\n";
116 $output .= '<noscript><img alt="Landkarte mit Rodelbahnen" src="' . htmlspecialchars($staticLink) . "\" width=\"$staticSizeX\" height=\"$staticSizeY\" /></noscript>\n";
118 return wrCommonReplaceByMarker($output, 'wrmap');
121 // returns a string that creates a map object called 'map'
122 private function addMap($mapName, $latitude, $longitude, $zoom) {
123 return "\tvar map = new GMap2(document.getElementById('$mapName'), {'mapTypes': [G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP]});\n" .
124 "\tmap.addControl(new GLargeMapControl());\n" .
125 "\tmap.addControl(new GMapTypeControl());\n" .
126 "\tmap.addControl(new GScaleControl());\n" .
127 "\tmap.setCenter(new GLatLng($latitude, $longitude), $zoom);\n" .
128 "\tmap.setMapType(G_PHYSICAL_MAP);\n" .
129 "\tmap.enableScrollWheelZoom();\n";
132 // returns a string with a add marker javascript call
133 private function addJsMarker($latitude, $longitude, $pageTitle) {
134 return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n";