3 function wrGeoParserFirstCallInit() {
5 $wgParser->setHook('geo', 'geoParserHook');
10 /// \brief This function converts a string like "47.236189 N 11.301471 E" to geographical coordinates in degrees.
13 /// try {list($latitude, $longitude) = wrGeoStringToGeo("47.236189 N 11.301471 E");}
14 /// catch (Exception $e) {return $e->getMessage();}
16 /// \throw exception if input is not formatted correctly with german UTF-8 error message. Don't forget to catch it!
17 /// \param UTF-8 encoded input string.
18 /// \return array($latitude, $longitude)
19 function wrGeoStringToGeo($input) {
20 if (!$input) throw new Exception(utf8_encode('Keine Koordinaten angegeben.'));
25 $n = sscanf($input, '%f %s %f %s', &$p1, &$p2, &$p3, &$p4);
26 if ($n != 4) throw new Exception(utf8_encode('Die Koordinaten sind falsch formatiert.'));
27 if ($p2 != 'N') return new Exception(utf8_encode('2. Parameter der Koordinaten muss N sein.'));
28 if ($p4 != 'E') return new Exception(utf8_encode('4. Parameter der Koordinaten muss E sein.'));
29 return array($p1, $p3);
33 function wrGeoGeoToString($latitude, $longitude) {
34 return sprintf('%f N %f E', $latitude, $longitude);
38 /// Format inside <geo>...</geo> has to be like this: <geo>47.236189 N 11.301471 E</geo>
39 function geoParserHook($input, $args, $parser) {
40 try {list($latitude, $longitude) = wrGeoStringToGeo($input);}
41 catch (Exception $e) {return '<em>' . htmlspecialchars($input) . '</em> <small>(' . htmlspecialchars($e->getMessage()) . ')</small>';}
42 return '<a href="http://maps.google.at/maps?q=' . sprintf('%F,%F', $latitude, $longitude) . '">' . wrGeoGeoToString($latitude, $longitude) . '</a>';