2 require_once dirname(__FILE__) . '/../wrgeo/wrgeo.body.php'; # for the function wrGeoGeoStringToGeo
4 function wrMapParserFirstCallInit() {
6 // global $wrGoogleMaps; // is this necessary?
7 global $wgGoogleMapsKey;
8 $wrGoogleMaps = new WrGoogleMaps($wgGoogleMapsKey);
9 $wgParser->setHook('wrmap', 'wrmapParserHook');
10 $wgParser->setHook('wrgmap', array($wrGoogleMaps, 'render'));
18 /// List of markers - used by the functions wrMapReplaceByMarker and wrMapAfterTidy
19 $wrMapMarkerList = array();
22 /// Returns a marker for a text and back-replaces the text in wrReportAfterTidy
23 function wrMapReplaceByMarker($text, $marker = 'wrMapMarker') {
24 $marker = $marker . mt_rand(1e5, 1e7);
25 global $wrMapMarkerList;
26 $wrMapMarkerList[$marker] = $text;
31 /// Replaces the markers by its contents
32 function wrMapParserAfterTidy(&$parser, &$text) {
33 // find markers in $text
34 // replace markers with actual output
35 global $wrMapMarkerList;
36 foreach ($wrMapMarkerList as $marker => $html) $text = str_replace($marker, $html, $text);
45 // Global JavaScript functions
46 define(WRGMAPJSFUNCTIONS, <<<JAVASCRIPT
47 <script type="text/javascript">
49 var wrSleddingIcon = new GIcon(G_DEFAULT_ICON);
50 wrSleddingIcon.image = "/vorlagen/gmap_rodelbahn_c.png";
51 wrSleddingIcon.shadow = "/vorlagen/gmap_rodelbahn_c_s.png";
52 wrSleddingIcon.iconSize = new GSize(17, 17);
53 wrSleddingIcon.shadowSize = new GSize(23, 23);
54 wrSleddingIcon.iconAnchor = new GPoint(9, 9);
55 wrSleddingIcon.infoWindowAnchor = new GPoint(9, 9);
58 function wrCreateMarker(latitude, longitude, name, icon) {
59 var point = new GLatLng(latitude, longitude);
60 var marker = new GMarker(point, icon);
61 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml("<strong>" + name + "</strong>");});
70 /** This class was inpired by the GoogleMaps class of the GoogleMaps extension. */
72 /// the Google API key (obtained from
73 /// http://www.google.com/apis/maps/signup.html)
74 private $apiKey = null;
76 /// How many <wrgmap> tags are on the current page?
77 private $mapsCount = 0;
80 function WrGoogleMaps($apiKey) {
81 $this->apiKey = $apiKey;
84 /// Renders the <wrgmap> tag
85 /// @param $content string - the content of the <wrgmap> tag
86 /// @param $args array - the array of attribute name/value pairs for the tag
87 /// @param $parser Parser - the MW Parser object for the current page
88 /// @param $localParser Parser - the parser for parsing local content
90 /// @return string - the html for rendering the map
91 function render($content, $args, &$parser) {
95 $dbr = wfGetDB(DB_SLAVE);
96 $res = $dbr->select('wrsleddingcache', array('page_title', 'position_latitude', 'position_longitude'));
97 $sleddingRoutes = array();
98 while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute;
99 $dbr->freeResult($res);
101 // Load Google Maps Script and define functions
103 if ($this->mapsCount == 1) {
104 $output .= '<script src="http://maps.google.com/maps?file=api&v=2&key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\n";
105 $output .= WRGMAPJSFUNCTIONS;
108 // Create <div/> element where the map is placed in
109 $mapName = 'wrgmap' . $this->mapsCount;
110 $output .= '<div id="' . $mapName . '" style="width: 100%; height: 450px; border-style:none;"></div>';
113 $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
114 $output .= 'if (GBrowserIsCompatible()) {' . "\n";
115 $output .= $this->addMap($mapName);
116 foreach ($sleddingRoutes as $s) {
117 $lat = $s['position_latitude'];
118 $lon = $s['position_longitude'];
119 $pageTitle = $s['page_title'];
120 if (!$lat || !$lon) continue;
121 $output .= $this->addJsMarker($lat, $lon, $pageTitle);
123 $output .= "}\n//]]>\n</script>\n";
124 return wrMapReplaceByMarker($output);
127 // returns a string that creates a map object called 'map'
128 private function addMap($mapName) {
129 return "\tvar map = new GMap2(document.getElementById('$mapName'), {'mapTypes': [G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP]});\n" .
130 "\tmap.addControl(new GLargeMapControl());\n" .
131 "\tmap.addControl(new GMapTypeControl());\n" .
132 "\tmap.addControl(new GScaleControl());\n" .
133 "\tmap.setCenter(new GLatLng(47.267648, 11.404655), 10);\n" .
134 "\tmap.setMapType(G_PHYSICAL_MAP);\n" .
135 "\tmap.enableScrollWheelZoom();\n";
138 // returns a string with a add marker javascript call
139 private function addJsMarker($latitude, $longitude, $pageTitle) {
140 return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n";
146 /// Format inside <wrmap>...</wrmap> has to be like this:
149 /// Rodelbahn|47.143241 N 11.208959 E|Birgitzer Alm
153 /// The extension produces a format like this:
154 /// <googlemap version="0.9" lat="47.241016" lon="11.56517" zoom="11">
155 /// 47.241731, 11.358994, Birgitzer Alm
156 /// Die Birgitzer Alm ist nett
157 /// 47.17607, 11.542763, Naviser Hütte
158 /// Die Naviser Hütte
160 function wrmapParserHook($input, $args, $parser) {
161 $debug = (isset($args['debug']) && $args['debug'] != '0');
164 $lines = explode("\n", $input);
165 $latitudes = array();
166 $longitudes = array();
167 foreach ($lines as $line) {
170 if (strlen($l) == 0) continue;
171 $columns = explode('|', $line);
172 if (count($columns) != 3) throw new Exception(sprintf(utf8_encode('Die Anzahl der Spalten ist nicht 3 sondern %d'), count($columns)));
173 $columns = list($type, $geo, $name) = $columns;
174 if (strlen(trim($geo)) == 0) continue;
175 list($latitude, $longitude) = wrGeoStringToGeo($geo);
176 $latitudes[] = $latitude;
177 $longitudes[] = $longitude;
178 $output .= sprintf("%F, %F, %s\n", $latitude, $longitude, htmlspecialchars($name));
179 } catch (Exception $e) {
180 return sprintf(utf8_encode('Ungültige Zeile in der Koordinatenliste: <em>%s</em>. %s'), htmlspecialchars($line), htmlspecialchars($e->getMessage()));
183 if (count($latitudes) == 0) return utf8_encode('Keine Koordinaten eingetragen');
184 $minLatitude = min($latitudes);
185 $centerLat = $minLatitude + (max($latitudes) - $minLatitude) / 2;
186 $minLongitude = min($longitudes);
187 $centerLon = $minLongitude + (max($longitudes) - $minLongitude) / 2;
189 // TODO: Varable zoom level
190 $output = '<googlemap version="0.9" lat="' . $centerLat . '" lon="' . $centerLon . '" type="terrain" zoom="9">'. "\n" . $output . "</googlemap>\n";
192 if ($debug) return "<pre><nowiki>$output</nowiki></pre>\n";
193 return $parser->recursiveTagParse($output);