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);
51 var wrSleddingIcon = new GIcon();
52 wrSleddingIcon.image = "bahn.png";
53 wrSleddingIcon.shadow = "bahns.png";
54 wrSleddingIcon.iconSize = new GSize(17, 17);
55 wrSleddingIcon.shadowSize = new GSize(23, 23);
56 wrSleddingIcon.iconAnchor = new GPoint(9, 9);
57 wrSleddingIcon.infoWindowAnchor = new GPoint(9, 9);
61 function wrCreateMarker(latitude, longitude, name, icon) {
62 var point = new GLatLng(latitude, longitude);
63 var marker = new GMarker(point, icon);
64 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml("<strong>" + name + "</strong>");});
73 /** This class was inpired by the GoogleMaps class of the GoogleMaps extension. */
75 /// the Google API key (obtained from
76 /// http://www.google.com/apis/maps/signup.html)
77 private $apiKey = null;
79 /// How many <wrgmap> tags are on the current page?
80 private $mapsCount = 0;
83 function WrGoogleMaps($apiKey) {
84 $this->apiKey = $apiKey;
87 /// Renders the <wrgmap> tag
88 /// @param $content string - the content of the <wrgmap> tag
89 /// @param $args array - the array of attribute name/value pairs for the tag
90 /// @param $parser Parser - the MW Parser object for the current page
91 /// @param $localParser Parser - the parser for parsing local content
93 /// @return string - the html for rendering the map
94 function render($content, $args, &$parser) {
98 $dbr = wfGetDB(DB_SLAVE);
99 $res = $dbr->select('wrsleddingcache', array('page_title', 'position_latitude', 'position_longitude'));
100 $sleddingRoutes = array();
101 while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute;
102 $dbr->freeResult($res);
104 // Load Google Maps Script and define functions
106 if ($this->mapsCount == 1) {
107 $output .= '<script src="http://maps.google.com/maps?file=api&v=2&key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\n";
108 $output .= WRGMAPJSFUNCTIONS;
111 // Create <div/> element where the map is placed in
112 $mapName = 'wrgmap' . $this->mapsCount;
113 $output .= '<div id="' . $mapName . '" style="width: 100%; height: 450px; border-style:none;"></div>';
116 $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
117 $output .= 'if (GBrowserIsCompatible()) {' . "\n";
118 $output .= $this->addMap($mapName);
119 foreach ($sleddingRoutes as $s) {
120 $lat = $s['position_latitude'];
121 $lon = $s['position_longitude'];
122 $pageTitle = $s['page_title'];
123 if (!$lat || !$lon) continue;
124 $output .= $this->addJsMarker($lat, $lon, $pageTitle);
126 $output .= "}\n//]]>\n</script>\n";
127 return wrMapReplaceByMarker($output);
130 // returns a string that creates a map object called 'map'
131 private function addMap($mapName) {
132 return "\tvar map = new GMap2(document.getElementById('$mapName'), {'mapTypes': [G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP]});\n" .
133 "\tmap.addControl(new GLargeMapControl());\n" .
134 "\tmap.addControl(new GMapTypeControl());\n" .
135 "\tmap.addControl(new GScaleControl());\n" .
136 "\tmap.setCenter(new GLatLng(47.267648, 11.404655), 10);\n" .
137 "\tmap.setMapType(G_PHYSICAL_MAP);\n" .
138 "\tmap.enableScrollWheelZoom();\n";
141 // returns a string with a add marker javascript call
142 private function addJsMarker($latitude, $longitude, $pageTitle) {
143 return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n";
149 /// Format inside <wrmap>...</wrmap> has to be like this:
152 /// Rodelbahn|47.143241 N 11.208959 E|Birgitzer Alm
156 /// The extension produces a format like this:
157 /// <googlemap version="0.9" lat="47.241016" lon="11.56517" zoom="11">
158 /// 47.241731, 11.358994, Birgitzer Alm
159 /// Die Birgitzer Alm ist nett
160 /// 47.17607, 11.542763, Naviser Hütte
161 /// Die Naviser Hütte
163 function wrmapParserHook($input, $args, $parser) {
164 $debug = (isset($args['debug']) && $args['debug'] != '0');
167 $lines = explode("\n", $input);
168 $latitudes = array();
169 $longitudes = array();
170 foreach ($lines as $line) {
173 if (strlen($l) == 0) continue;
174 $columns = explode('|', $line);
175 if (count($columns) != 3) throw new Exception(sprintf(utf8_encode('Die Anzahl der Spalten ist nicht 3 sondern %d'), count($columns)));
176 $columns = list($type, $geo, $name) = $columns;
177 if (strlen(trim($geo)) == 0) continue;
178 list($latitude, $longitude) = wrGeoStringToGeo($geo);
179 $latitudes[] = $latitude;
180 $longitudes[] = $longitude;
181 $output .= sprintf("%F, %F, %s\n", $latitude, $longitude, htmlspecialchars($name));
182 } catch (Exception $e) {
183 return sprintf(utf8_encode('Ungültige Zeile in der Koordinatenliste: <em>%s</em>. %s'), htmlspecialchars($line), htmlspecialchars($e->getMessage()));
186 if (count($latitudes) == 0) return utf8_encode('Keine Koordinaten eingetragen');
187 $minLatitude = min($latitudes);
188 $centerLat = $minLatitude + (max($latitudes) - $minLatitude) / 2;
189 $minLongitude = min($longitudes);
190 $centerLon = $minLongitude + (max($longitudes) - $minLongitude) / 2;
192 // TODO: Varable zoom level
193 $output = '<googlemap version="0.9" lat="' . $centerLat . '" lon="' . $centerLon . '" type="terrain" zoom="9">'. "\n" . $output . "</googlemap>\n";
195 if ($debug) return "<pre><nowiki>$output</nowiki></pre>\n";
196 return $parser->recursiveTagParse($output);