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();
51 wrSleddingIcon.image = "bahn.png";
52 wrSleddingIcon.shadow = "bahns.png";
53 wrSleddingIcon.iconSize = new GSize(17, 17);
54 wrSleddingIcon.shadowSize = new GSize(23, 23);
55 wrSleddingIcon.iconAnchor = new GPoint(9, 9);
56 wrSleddingIcon.infoWindowAnchor = new GPoint(9, 9);
60 function wrCreateMarker(latitude, longitude, name, icon) {
61 var point = new GLatLng(latitude, longitude);
62 var marker = new GMarker(point, icon);
63 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml("<strong>" + name + "</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
90 /// @param $localParser Parser - the parser for parsing local content
92 /// @return string - the html for rendering the map
93 function render($content, $args, &$parser) {
97 $dbr = wfGetDB(DB_SLAVE);
98 $res = $dbr->select('wrsleddingcache', array('page_title', 'position_latitude', 'position_longitude'));
99 $sleddingRoutes = array();
100 while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute;
101 $dbr->freeResult($res);
103 // Load Google Maps Script and define functions
105 if ($this->mapsCount == 1) {
106 $output .= '<script src="http://maps.google.com/maps?file=api&v=2&key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\n";
107 $output .= WRGMAPJSFUNCTIONS;
110 // Create <div/> element where the map is placed in
111 $mapName = 'wrgmap' . $this->mapsCount;
112 $output .= '<div id="' . $mapName . '" style="width: 700px; height: 450px; border-style:solid; border-width:medium; border-color:blue;"></div>';
115 $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
116 $output .= 'if (GBrowserIsCompatible()) {' . "\n";
117 $output .= $this->addMap($mapName);
118 foreach ($sleddingRoutes as $s) {
119 $lat = $s['position_latitude'];
120 $lon = $s['position_longitude'];
121 $pageTitle = $s['page_title'];
122 if (!$lat || !$lon) continue;
123 $output .= $this->addJsMarker($lat, $lon, $pageTitle);
125 $output .= "}\n//]]>\n</script>\n";
126 return wrMapReplaceByMarker($output);
129 // returns a string that creates a map object called 'map'
130 private function addMap($mapName) {
131 return "\tmap = new GMap2(document.getElementById(\"$mapName\"));\n" .
132 "\tmap.addControl(new GLargeMapControl());\n" .
133 "\tmap.addControl(new GMapTypeControl());\n" .
134 "\tmap.addControl(new GScaleControl());\n" .
135 "\tmap.setCenter(new GLatLng(47.267648, 11.404655), 10);\n" .
136 "\tmap.setMapType(G_SATELLITE_MAP);\n" .
137 "\tmap.enableScrollWheelZoom();\n";
140 // returns a string with a add marker javascript call
141 private function addJsMarker($latitude, $longitude, $pageTitle) {
142 return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n";
148 /// Format inside <wrmap>...</wrmap> has to be like this:
151 /// Rodelbahn|47.143241 N 11.208959 E|Birgitzer Alm
155 /// The extension produces a format like this:
156 /// <googlemap version="0.9" lat="47.241016" lon="11.56517" zoom="11">
157 /// 47.241731, 11.358994, Birgitzer Alm
158 /// Die Birgitzer Alm ist nett
159 /// 47.17607, 11.542763, Naviser Hütte
160 /// Die Naviser Hütte
162 function wrmapParserHook($input, $args, $parser) {
163 $debug = (isset($args['debug']) && $args['debug'] != '0');
166 $lines = explode("\n", $input);
167 $latitudes = array();
168 $longitudes = array();
169 foreach ($lines as $line) {
172 if (strlen($l) == 0) continue;
173 $columns = explode('|', $line);
174 if (count($columns) != 3) throw new Exception(sprintf(utf8_encode('Die Anzahl der Spalten ist nicht 3 sondern %d'), count($columns)));
175 $columns = list($type, $geo, $name) = $columns;
176 if (strlen(trim($geo)) == 0) continue;
177 list($latitude, $longitude) = wrGeoStringToGeo($geo);
178 $latitudes[] = $latitude;
179 $longitudes[] = $longitude;
180 $output .= sprintf("%F, %F, %s\n", $latitude, $longitude, htmlspecialchars($name));
181 } catch (Exception $e) {
182 return sprintf(utf8_encode('Ungültige Zeile in der Koordinatenliste: <em>%s</em>. %s'), htmlspecialchars($line), htmlspecialchars($e->getMessage()));
185 if (count($latitudes) == 0) return utf8_encode('Keine Koordinaten eingetragen');
186 $minLatitude = min($latitudes);
187 $centerLat = $minLatitude + (max($latitudes) - $minLatitude) / 2;
188 $minLongitude = min($longitudes);
189 $centerLon = $minLongitude + (max($longitudes) - $minLongitude) / 2;
191 // TODO: Varable zoom level
192 $output = '<googlemap version="0.9" lat="' . $centerLat . '" lon="' . $centerLon . '" type="terrain" zoom="9">'. "\n" . $output . "</googlemap>\n";
194 if ($debug) return "<pre><nowiki>$output</nowiki></pre>\n";
195 return $parser->recursiveTagParse($output);