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 var articlePath = "$wgArticlePath";
62 var p = articlePath.replace("\$1", name.replace(' ', '_'));
63 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml("<strong><a href='" + p + "'>" + name+ "</a></strong>");});
72 /** This class was inpired by the GoogleMaps class of the GoogleMaps extension. */
74 /// the Google API key (obtained from
75 /// http://www.google.com/apis/maps/signup.html)
76 private $apiKey = null;
78 /// How many <wrgmap> tags are on the current page?
79 private $mapsCount = 0;
82 function WrGoogleMaps($apiKey) {
83 $this->apiKey = $apiKey;
86 /// Renders the <wrgmap> tag
87 /// @param $content string - the content of the <wrgmap> tag
88 /// @param $args array - the array of attribute name/value pairs for the tag
89 /// @param $parser Parser - the MW Parser object for the current page
91 /// @return string - the html for rendering the map
92 function render($content, $args, &$parser) {
96 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648;
97 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655;
98 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
103 $dbr = wfGetDB(DB_SLAVE);
104 $res = $dbr->select('wrsleddingcache', array('page_title', 'position_latitude', 'position_longitude'));
105 $sleddingRoutes = array();
106 while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute;
107 $dbr->freeResult($res);
109 // Load Google Maps Script and define functions
111 if ($this->mapsCount == 1) {
112 $output .= '<script src="http://maps.google.com/maps?file=api&v=2&key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\n";
113 $output .= WRGMAPJSFUNCTIONS;
116 // Create static map image link
117 $staticLink = "http://maps.google.com/staticmap?center=$latitude,$longitude&zoom=$zoom&size=${staticSizeX}x$staticSizeY&key=$this->apiKey";
118 $staticMarkers = array();
119 foreach ($sleddingRoutes as $s) {
120 $lat = $s['position_latitude'];
121 $lon = $s['position_longitude'];
122 $staticMarkers[] = sprintf('%.3f,%.3f,bluer', $lat, $lon);
124 if (count($staticMarkers) > 0) $staticLink .= '&' . implode('|', $staticMarkers);
126 // Create <div/> element where the map is placed in
127 $mapName = 'wrgmap' . $this->mapsCount;
128 $output .= '<div id="' . $mapName . '" style="width: 100%; height: 450px; border-style:none;"></div>';
131 $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
132 $output .= 'if (GBrowserIsCompatible()) {' . "\n";
133 $output .= $this->addMap($mapName, $latitude, $longitude, $zoom);
134 foreach ($sleddingRoutes as $s) {
135 $lat = $s['position_latitude'];
136 $lon = $s['position_longitude'];
137 $pageTitle = $s['page_title'];
138 if (!$lat || !$lon) continue;
139 $output .= $this->addJsMarker($lat, $lon, $pageTitle);
141 $output .= "} else {\n"; // browser not compatible -> create static map
142 $output .= "\tdocument.write(\"<img alt=\\\"Landkarte mit Rodelbahnen\\\" src=\\\"" . htmlspecialchars($staticLink) . "\\\" width=\\\"$staticSizeX\\\" height=\\\"$staticSizeY\\\" />\");\n";
143 $output .= "}\n//]]>\n</script>\n";
144 $output .= '<noscript><img alt="Landkarte mit Rodelbahnen" src="' . htmlspecialchars($staticLink) . "\" width=\"$staticSizeX\" height=\"$staticSizeY\" /></noscript>\n";
146 return wrMapReplaceByMarker($output);
149 // returns a string that creates a map object called 'map'
150 private function addMap($mapName, $latitude, $longitude, $zoom) {
151 return "\tvar map = new GMap2(document.getElementById('$mapName'), {'mapTypes': [G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP]});\n" .
152 "\tmap.addControl(new GLargeMapControl());\n" .
153 "\tmap.addControl(new GMapTypeControl());\n" .
154 "\tmap.addControl(new GScaleControl());\n" .
155 "\tmap.setCenter(new GLatLng($latitude, $longitude), $zoom);\n" .
156 "\tmap.setMapType(G_PHYSICAL_MAP);\n" .
157 "\tmap.enableScrollWheelZoom();\n";
160 // returns a string with a add marker javascript call
161 private function addJsMarker($latitude, $longitude, $pageTitle) {
162 return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n";
168 /// Format inside <wrmap>...</wrmap> has to be like this:
171 /// Rodelbahn|47.143241 N 11.208959 E|Birgitzer Alm
175 /// The extension produces a format like this:
176 /// <googlemap version="0.9" lat="47.241016" lon="11.56517" zoom="11">
177 /// 47.241731, 11.358994, Birgitzer Alm
178 /// Die Birgitzer Alm ist nett
179 /// 47.17607, 11.542763, Naviser Hütte
180 /// Die Naviser Hütte
182 function wrmapParserHook($input, $args, $parser) {
183 $debug = (isset($args['debug']) && $args['debug'] != '0');
186 $lines = explode("\n", $input);
187 $latitudes = array();
188 $longitudes = array();
189 foreach ($lines as $line) {
192 if (strlen($l) == 0) continue;
193 $columns = explode('|', $line);
194 if (count($columns) != 3) throw new Exception(sprintf(utf8_encode('Die Anzahl der Spalten ist nicht 3 sondern %d'), count($columns)));
195 $columns = list($type, $geo, $name) = $columns;
196 if (strlen(trim($geo)) == 0) continue;
197 list($latitude, $longitude) = wrGeoStringToGeo($geo);
198 $latitudes[] = $latitude;
199 $longitudes[] = $longitude;
200 $output .= sprintf("%F, %F, %s\n", $latitude, $longitude, htmlspecialchars($name));
201 } catch (Exception $e) {
202 return sprintf(utf8_encode('Ungültige Zeile in der Koordinatenliste: <em>%s</em>. %s'), htmlspecialchars($line), htmlspecialchars($e->getMessage()));
205 if (count($latitudes) == 0) return utf8_encode('Keine Koordinaten eingetragen');
206 $minLatitude = min($latitudes);
207 $centerLat = $minLatitude + (max($latitudes) - $minLatitude) / 2;
208 $minLongitude = min($longitudes);
209 $centerLon = $minLongitude + (max($longitudes) - $minLongitude) / 2;
211 // TODO: Varable zoom level
212 $output = '<googlemap version="0.9" lat="' . $centerLat . '" lon="' . $centerLon . '" type="terrain" zoom="9">'. "\n" . $output . "</googlemap>\n";
214 if ($debug) return "<pre><nowiki>$output</nowiki></pre>\n";
215 return $parser->recursiveTagParse($output);