|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class for geocoding requests with the Google Geocoding Service (v3). |
|
5
|
|
|
* |
|
6
|
|
|
* Webservice documentation: http://code.google.com/apis/maps/documentation/geocoding/ |
|
7
|
|
|
* |
|
8
|
|
|
* @licence GNU GPL v2+ |
|
9
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
10
|
|
|
* @author Sergey Chernyshev |
|
11
|
|
|
* @author Desiree Gennaro |
|
12
|
|
|
*/ |
|
13
|
|
|
final class MapsGoogleGeocoder extends \Maps\Geocoder { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Registers the geocoder. |
|
17
|
|
|
* |
|
18
|
|
|
* No LSB in pre-5.3 PHP *sigh*. |
|
19
|
|
|
* This is to be refactored as soon as php >=5.3 becomes acceptable. |
|
20
|
|
|
* |
|
21
|
|
|
* @since 0.7 |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function register() { |
|
24
|
|
|
\Maps\Geocoders::registerGeocoder( 'google', __CLASS__ ); |
|
25
|
|
|
return true; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @see \Maps\Geocoder::getRequestUrl |
|
30
|
|
|
* |
|
31
|
|
|
* @since 0.7 |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $address |
|
34
|
|
|
* |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function getRequestUrl( $address ) { |
|
38
|
|
|
$urlArgs = [ |
|
39
|
|
|
'address' => urlencode( $address ), |
|
40
|
|
|
'sensor' => false |
|
41
|
|
|
]; |
|
42
|
|
|
if ( !empty( $GLOBALS['egMapsGMaps3ApiKey'] ) ) { |
|
43
|
|
|
$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey'] |
|
44
|
|
|
} |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
return 'http://maps.googleapis.com/maps/api/geocode/xml?' . wfArrayToCgi($urlArgs); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @see \Maps\Geocoder::parseResponse |
|
51
|
|
|
* |
|
52
|
|
|
* @since 0.7 |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $response |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function parseResponse( $response ) { |
|
59
|
|
|
$lon = self::getXmlElementValue( $response, 'lng' ); |
|
60
|
|
|
$lat = self::getXmlElementValue( $response, 'lat' ); |
|
61
|
|
|
|
|
62
|
|
|
// In case on of the values is not found, return false. |
|
63
|
|
|
if ( !$lon || !$lat ) return false; |
|
64
|
|
|
|
|
65
|
|
|
return [ |
|
66
|
|
|
'lat' => (float)$lat, |
|
67
|
|
|
'lon' => (float)$lon |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @see \Maps\Geocoder::getOverrides |
|
73
|
|
|
* |
|
74
|
|
|
* @since 0.7 |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function getOverrides() { |
|
79
|
|
|
return [ 'googlemaps3' ]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|