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
101 $dbr = wfGetDB(DB_SLAVE);
102 $res = $dbr->select('wrsleddingcache', array('page_title', 'position_latitude', 'position_longitude'));
103 $sleddingRoutes = array();
104 while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute;
105 $dbr->freeResult($res);
107 // Load Google Maps Script and define functions
109 if ($this->mapsCount == 1) {
110 $output .= '<script src="http://maps.google.com/maps?file=api&v=2&key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\n";
111 $output .= WRGMAPJSFUNCTIONS;
114 // Create <div/> element where the map is placed in
115 $mapName = 'wrgmap' . $this->mapsCount;
116 $output .= '<div id="' . $mapName . '" style="width: 100%; height: 450px; border-style:none;"></div>';
119 $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
120 $output .= 'if (GBrowserIsCompatible()) {' . "\n";
121 $output .= $this->addMap($mapName, $latitude, $longitude, $zoom);
122 foreach ($sleddingRoutes as $s) {
123 $lat = $s['position_latitude'];
124 $lon = $s['position_longitude'];
125 $pageTitle = $s['page_title'];
126 if (!$lat || !$lon) continue;
127 $output .= $this->addJsMarker($lat, $lon, $pageTitle);
129 $output .= "}\n//]]>\n</script>\n";
130 return wrMapReplaceByMarker($output);
133 // returns a string that creates a map object called 'map'
134 private function addMap($mapName, $latitude, $longitude, $zoom) {
135 return "\tvar map = new GMap2(document.getElementById('$mapName'), {'mapTypes': [G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP]});\n" .
136 "\tmap.addControl(new GLargeMapControl());\n" .
137 "\tmap.addControl(new GMapTypeControl());\n" .
138 "\tmap.addControl(new GScaleControl());\n" .
139 "\tmap.setCenter(new GLatLng($latitude, $longitude), $zoom);\n" .
140 "\tmap.setMapType(G_PHYSICAL_MAP);\n" .
141 "\tmap.enableScrollWheelZoom();\n";
144 // returns a string with a add marker javascript call
145 private function addJsMarker($latitude, $longitude, $pageTitle) {
146 return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n";
152 /// Format inside <wrmap>...</wrmap> has to be like this:
155 /// Rodelbahn|47.143241 N 11.208959 E|Birgitzer Alm
159 /// The extension produces a format like this:
160 /// <googlemap version="0.9" lat="47.241016" lon="11.56517" zoom="11">
161 /// 47.241731, 11.358994, Birgitzer Alm
162 /// Die Birgitzer Alm ist nett
163 /// 47.17607, 11.542763, Naviser Hütte
164 /// Die Naviser Hütte
166 function wrmapParserHook($input, $args, $parser) {
167 $debug = (isset($args['debug']) && $args['debug'] != '0');
170 $lines = explode("\n", $input);
171 $latitudes = array();
172 $longitudes = array();
173 foreach ($lines as $line) {
176 if (strlen($l) == 0) continue;
177 $columns = explode('|', $line);
178 if (count($columns) != 3) throw new Exception(sprintf(utf8_encode('Die Anzahl der Spalten ist nicht 3 sondern %d'), count($columns)));
179 $columns = list($type, $geo, $name) = $columns;
180 if (strlen(trim($geo)) == 0) continue;
181 list($latitude, $longitude) = wrGeoStringToGeo($geo);
182 $latitudes[] = $latitude;
183 $longitudes[] = $longitude;
184 $output .= sprintf("%F, %F, %s\n", $latitude, $longitude, htmlspecialchars($name));
185 } catch (Exception $e) {
186 return sprintf(utf8_encode('Ungültige Zeile in der Koordinatenliste: <em>%s</em>. %s'), htmlspecialchars($line), htmlspecialchars($e->getMessage()));
189 if (count($latitudes) == 0) return utf8_encode('Keine Koordinaten eingetragen');
190 $minLatitude = min($latitudes);
191 $centerLat = $minLatitude + (max($latitudes) - $minLatitude) / 2;
192 $minLongitude = min($longitudes);
193 $centerLon = $minLongitude + (max($longitudes) - $minLongitude) / 2;
195 // TODO: Varable zoom level
196 $output = '<googlemap version="0.9" lat="' . $centerLat . '" lon="' . $centerLon . '" type="terrain" zoom="9">'. "\n" . $output . "</googlemap>\n";
198 if ($debug) return "<pre><nowiki>$output</nowiki></pre>\n";
199 return $parser->recursiveTagParse($output);