3 function wrMapParserFirstCallInit() {
5 global $wgGoogleMapsKey;
6 $wrGoogleMaps = new WrGoogleMaps($wgGoogleMapsKey);
7 $wgParser->setHook('wrmap', 'wrmapParserHook');
8 $wgParser->setHook('wrgmap', array($wrGoogleMaps, 'render'));
17 // Global JavaScript functions
18 define(WRGMAPJSFUNCTIONS, <<<JAVASCRIPT
19 <script type="text/javascript">
21 var wrSleddingIcon = new GIcon(G_DEFAULT_ICON);
22 wrSleddingIcon.image = "/vorlagen/gmap_rodelbahn_c.png";
23 wrSleddingIcon.shadow = "/vorlagen/gmap_rodelbahn_c_s.png";
24 wrSleddingIcon.iconSize = new GSize(17, 17);
25 wrSleddingIcon.shadowSize = new GSize(23, 23);
26 wrSleddingIcon.iconAnchor = new GPoint(9, 9);
27 wrSleddingIcon.infoWindowAnchor = new GPoint(9, 9);
30 function wrCreateMarker(latitude, longitude, name, icon) {
31 var point = new GLatLng(latitude, longitude);
32 var marker = new GMarker(point, icon);
33 var articlePath = "$wgArticlePath";
34 var p = articlePath.replace("\$1", name.replace(' ', '_'));
35 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml("<strong><a href='" + p + "'>" + name+ "</a></strong>");});
44 /** This class was inpired by the GoogleMaps class of the GoogleMaps extension. */
46 /// the Google API key (obtained from
47 /// http://www.google.com/apis/maps/signup.html)
48 private $apiKey = null;
50 /// How many <wrgmap> tags are on the current page?
51 private $mapsCount = 0;
54 function WrGoogleMaps($apiKey) {
55 $this->apiKey = $apiKey;
58 /// Renders the <wrgmap> tag
59 /// @param $content string - the content of the <wrgmap> tag
60 /// @param $args array - the array of attribute name/value pairs for the tag
61 /// @param $parser Parser - the MW Parser object for the current page
63 /// @return string - the html for rendering the map
64 function render($content, $args, &$parser) {
68 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648;
69 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655;
70 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
75 $dbr = wfGetDB(DB_SLAVE);
76 $res = $dbr->select('wrsleddingcache', array('page_title', 'position_latitude', 'position_longitude'), 'show_in_overview');
77 $sleddingRoutes = array();
78 while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute;
79 $dbr->freeResult($res);
81 // Load Google Maps Script and define functions
83 if ($this->mapsCount == 1) {
84 $output .= '<script src="http://maps.google.com/maps?file=api&v=2&key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\n";
85 $output .= WRGMAPJSFUNCTIONS;
88 // Create static map image link
89 $staticLink = "http://maps.google.com/staticmap?center=$latitude,$longitude&zoom=$zoom&size=${staticSizeX}x$staticSizeY&key=$this->apiKey";
90 $staticMarkers = array();
91 foreach ($sleddingRoutes as $s) {
92 $lat = $s['position_latitude'];
93 $lon = $s['position_longitude'];
94 $staticMarkers[] = sprintf('%.3f,%.3f,bluer', $lat, $lon);
96 if (count($staticMarkers) > 0) $staticLink .= '&markers=' . implode('|', $staticMarkers);
98 // Create <div/> element where the map is placed in
99 $mapName = 'wrgmap' . $this->mapsCount;
100 $output .= '<div id="' . $mapName . '" style="width: 100%; height: 450px; border-style:none; display:none;"></div>';
103 $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
104 $output .= 'if (GBrowserIsCompatible()) {' . "\n";
105 $output .= "\tdocument.getElementById(\"$mapName\").style.display = \"block\";\n";
106 $output .= $this->addMap($mapName, $latitude, $longitude, $zoom);
107 foreach ($sleddingRoutes as $s) {
108 $lat = $s['position_latitude'];
109 $lon = $s['position_longitude'];
110 $pageTitle = $s['page_title'];
111 if (!$lat || !$lon) continue;
112 $output .= $this->addJsMarker($lat, $lon, $pageTitle);
114 $output .= "} else {\n"; // browser not compatible -> create static map
115 $output .= "\tdocument.write(\"<img alt=\\\"Landkarte mit Rodelbahnen\\\" src=\\\"" . htmlspecialchars($staticLink) . "\\\" width=\\\"$staticSizeX\\\" height=\\\"$staticSizeY\\\" />\");\n";
116 $output .= "}\n//]]>\n</script>\n";
117 $output .= '<noscript><img alt="Landkarte mit Rodelbahnen" src="' . htmlspecialchars($staticLink) . "\" width=\"$staticSizeX\" height=\"$staticSizeY\" /></noscript>\n";
119 return wrCommonReplaceByMarker($output, 'wrmap');
122 // returns a string that creates a map object called 'map'
123 private function addMap($mapName, $latitude, $longitude, $zoom) {
124 return "\tvar map = new GMap2(document.getElementById('$mapName'), {'mapTypes': [G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP]});\n" .
125 "\tmap.addControl(new GLargeMapControl());\n" .
126 "\tmap.addControl(new GMapTypeControl());\n" .
127 "\tmap.addControl(new GScaleControl());\n" .
128 "\tmap.setCenter(new GLatLng($latitude, $longitude), $zoom);\n" .
129 "\tmap.setMapType(G_PHYSICAL_MAP);\n" .
130 "\tmap.enableScrollWheelZoom();\n";
133 // returns a string with a add marker javascript call
134 private function addJsMarker($latitude, $longitude, $pageTitle) {
135 return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n";
141 /// Format inside <wrmap>...</wrmap> has to be like this:
144 /// Rodelbahn|47.143241 N 11.208959 E|Birgitzer Alm
148 /// The extension produces a format like this:
149 /// <googlemap version="0.9" lat="47.241016" lon="11.56517" zoom="11">
150 /// 47.241731, 11.358994, Birgitzer Alm
151 /// Die Birgitzer Alm ist nett
152 /// 47.17607, 11.542763, Naviser Hütte
153 /// Die Naviser Hütte
155 function wrmapParserHook($input, $args, $parser) {
156 $debug = (isset($args['debug']) && $args['debug'] != '0');
159 $lines = explode("\n", $input);
160 $latitudes = array();
161 $longitudes = array();
162 foreach ($lines as $line) {
165 if (strlen($l) == 0) continue;
166 $columns = explode('|', $line);
167 if (count($columns) != 3) throw new Exception(sprintf(utf8_encode('Die Anzahl der Spalten ist nicht 3 sondern %d'), count($columns)));
168 $columns = list($type, $geo, $name) = $columns;
169 if (strlen(trim($geo)) == 0) continue;
170 list($latitude, $longitude) = wrGeoStringToGeo($geo);
171 $latitudes[] = $latitude;
172 $longitudes[] = $longitude;
173 $output .= sprintf("%F, %F, %s\n", $latitude, $longitude, htmlspecialchars($name));
174 } catch (Exception $e) {
175 return sprintf(utf8_encode('Ungültige Zeile in der Koordinatenliste: <em>%s</em>. %s'), htmlspecialchars($line), htmlspecialchars($e->getMessage()));
178 if (count($latitudes) == 0) return utf8_encode('Keine Koordinaten eingetragen');
179 $minLatitude = min($latitudes);
180 $centerLat = $minLatitude + (max($latitudes) - $minLatitude) / 2;
181 $minLongitude = min($longitudes);
182 $centerLon = $minLongitude + (max($longitudes) - $minLongitude) / 2;
184 // TODO: Varable zoom level
185 $output = '<googlemap version="0.9" lat="' . $centerLat . '" lon="' . $centerLon . '" type="terrain" zoom="9">'. "\n" . $output . "</googlemap>\n";
187 if ($debug) return "<pre><nowiki>$output</nowiki></pre>\n";
188 return $parser->recursiveTagParse($output);