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
|
|
|
]; |
41
|
|
|
if ( $GLOBALS['egMapsGMaps3ApiKey'] !== '' ) { |
42
|
|
|
$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey']; |
43
|
|
|
} |
44
|
|
|
if ( $GLOBALS['egMapsGMaps3ApiVersion'] !== '' ) { |
45
|
|
|
$urlArgs['v'] = $GLOBALS['egMapsGMaps3ApiVersion']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return 'https://maps.googleapis.com/maps/api/geocode/xml?' . wfArrayToCgi($urlArgs); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @see \Maps\Geocoder::parseResponse |
53
|
|
|
* |
54
|
|
|
* @since 0.7 |
55
|
|
|
* |
56
|
|
|
* @param string $response |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
protected function parseResponse( $response ) { |
61
|
|
|
$lon = self::getXmlElementValue( $response, 'lng' ); |
62
|
|
|
$lat = self::getXmlElementValue( $response, 'lat' ); |
63
|
|
|
|
64
|
|
|
// In case on of the values is not found, return false. |
65
|
|
|
if ( !$lon || !$lat ) return false; |
|
|
|
|
66
|
|
|
|
67
|
|
|
return [ |
68
|
|
|
'lat' => (float)$lat, |
69
|
|
|
'lon' => (float)$lon |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @see \Maps\Geocoder::getOverrides |
75
|
|
|
* |
76
|
|
|
* @since 0.7 |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public static function getOverrides() { |
81
|
|
|
return [ 'googlemaps3' ]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: