3 function wrMapParserFirstCallInit() {
5 global $wgGoogleMapsKey;
6 $wrGoogleMaps = new WrGoogleMaps($wgGoogleMapsKey);
7 $wgParser->setHook('wrgmap', array($wrGoogleMaps, 'render'));
16 // Global JavaScript functions
17 define('WRGMAPJSFUNCTIONS', <<<JAVASCRIPT
18 <script type="text/javascript">
20 var wrSleddingIcon = new GIcon(G_DEFAULT_ICON);
21 wrSleddingIcon.image = "/vorlagen/gmap_rodelbahn_c.png";
22 wrSleddingIcon.shadow = "/vorlagen/gmap_rodelbahn_c_s.png";
23 wrSleddingIcon.iconSize = new GSize(17, 17);
24 wrSleddingIcon.shadowSize = new GSize(23, 23);
25 wrSleddingIcon.iconAnchor = new GPoint(9, 9);
26 wrSleddingIcon.infoWindowAnchor = new GPoint(9, 9);
29 function wrCreateMarker(latitude, longitude, name, icon) {
30 var point = new GLatLng(latitude, longitude);
31 var marker = new GMarker(point, icon);
32 var articlePath = "$wgArticlePath";
33 var p = articlePath.replace("\$1", name.replace(' ', '_'));
34 GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml("<strong><a href='" + p + "'>" + name+ "</a></strong>");});
43 /** This class was inpired by the GoogleMaps class of the GoogleMaps extension. */
45 /// the Google API key (obtained from
46 /// http://www.google.com/apis/maps/signup.html)
47 private $apiKey = null;
49 /// How many <wrgmap> tags are on the current page?
50 private $mapsCount = 0;
53 function WrGoogleMaps($apiKey) {
54 $this->apiKey = $apiKey;
57 /// Renders the <wrgmap> tag
58 /// @param $content string - the content of the <wrgmap> tag
59 /// @param $args array - the array of attribute name/value pairs for the tag
60 /// @param $parser Parser - the MW Parser object for the current page
62 /// @return string - the html for rendering the map
63 function render($content, $args, $parser) {
67 if (isset($args['lat'])) $latitude = floatval($args['lat']); else $latitude = 47.267648; // latitude as float value
68 if (isset($args['lon'])) $longitude = floatval($args['lon']); else $longitude = 11.404655; // longitude as float value
69 if (isset($args['zoom'])) $zoom = intval($args['zoom']); else $zoom = 10; // Google Zoom Level
72 $latitude_s = sprintf('%.6F', $latitude);
73 $longitude_s = sprintf('%.6F', $longitude);
76 $dbr = wfGetDB(DB_SLAVE);
77 $res = $dbr->select('wrsleddingcache', array('page_title', 'position_latitude', 'position_longitude'), array('show_in_overview', 'not under_construction'));
78 $sleddingRoutes = array();
79 while ($sleddingRoute = $dbr->fetchRow($res)) $sleddingRoutes[] = $sleddingRoute;
80 $dbr->freeResult($res);
82 // Load Google Maps Script and define functions
84 if ($this->mapsCount == 1) {
85 $output .= '<script src="http://maps.google.com/maps?file=api&v=2&key=' . htmlspecialchars($this->apiKey) . '" type="text/javascript"></script>' . "\n";
86 $output .= WRGMAPJSFUNCTIONS;
89 // Create static map image link
90 $staticLink = "http://maps.google.com/staticmap?center=$latitude_s,$longitude_s&zoom=$zoom&size=${staticSizeX}x$staticSizeY&key=$this->apiKey";
91 $staticMarkers = array();
92 foreach ($sleddingRoutes as $s) {
93 $lat = $s['position_latitude']; // this is a string
94 $lon = $s['position_longitude']; // this is a string
95 $staticMarkers[] = sprintf('%s,%s,bluer', $lat, $lon);
97 if (count($staticMarkers) > 0) $staticLink .= '&markers=' . implode('|', $staticMarkers);
99 // Create <div/> element where the map is placed in
100 $mapName = 'wrgmap' . $this->mapsCount;
101 $output .= '<div id="' . $mapName . '" style="width: 100%; height: 450px; border-style:none; display:none;"></div>';
104 $output .= '<script type="text/javascript">' . "\n//<![CDATA[\n";
105 $output .= 'if (GBrowserIsCompatible()) {' . "\n";
106 $output .= "\tdocument.getElementById(\"$mapName\").style.display = \"block\";\n";
107 $output .= $this->addMap($mapName, $latitude, $longitude, $zoom);
108 foreach ($sleddingRoutes as $s) {
109 $lat = $s['position_latitude'];
110 $lon = $s['position_longitude'];
111 $pageTitle = $s['page_title'];
112 if (!$lat || !$lon) continue;
113 $output .= $this->addJsMarker($lat, $lon, $pageTitle);
115 $output .= "} else {\n"; // browser not compatible -> create static map
116 $output .= "\tdocument.write(\"<img alt=\\\"Landkarte mit Rodelbahnen\\\" src=\\\"" . htmlspecialchars($staticLink) . "\\\" width=\\\"$staticSizeX\\\" height=\\\"$staticSizeY\\\" />\");\n";
117 $output .= "}\n//]]>\n</script>\n";
118 $output .= '<noscript><img alt="Landkarte mit Rodelbahnen" src="' . htmlspecialchars($staticLink) . "\" width=\"$staticSizeX\" height=\"$staticSizeY\" /></noscript>\n";
120 return wrCommonReplaceByMarker($output, 'wrmap');
123 // returns a string that creates a map object called 'map'
124 // $latitude, $longitude and $zoom have to be float/integer values.
125 private function addMap($mapName, $latitude, $longitude, $zoom) {
126 return "\tvar map = new GMap2(document.getElementById('$mapName'), {'mapTypes': [G_NORMAL_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP, G_SATELLITE_MAP]});\n" .
127 "\tmap.addControl(new GLargeMapControl());\n" .
128 "\tmap.addControl(new GMapTypeControl());\n" .
129 "\tmap.addControl(new GScaleControl());\n" .
130 sprintf("\tmap.setCenter(new GLatLng(%.6F, %.6F), %d);\n", $latitude, $longitude, $zoom) .
131 "\tmap.setMapType(G_PHYSICAL_MAP);\n";
132 // "\tmap.enableScrollWheelZoom();\n";
135 // returns a string with a add marker javascript call
136 // $latitude and $longitude are expected as string
137 private function addJsMarker($latitude, $longitude, $pageTitle) {
138 return "\tmap.addOverlay(wrCreateMarker($latitude, $longitude, \"" . htmlspecialchars(addslashes($pageTitle)) . "\", wrSleddingIcon));\n";