class-geolocation.php
4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Geolocation class.
*
* @category Locations
* @package My Calendar
* @author Joe Dolson
* @license GPLv2 or later
* @link https://www.joedolson.com/my-calendar/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Geolocation
*
* Get latitude/longitude or address using Google Maps API
*
* @author Jeroen Desloovere <info@jeroendesloovere.be>
* @modified Joe Dolson <plugins@joedolson.com> Converted to use WP HTTP.
*/
class Geolocation {
// API URL.
const API_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
/**
* Do call
*
* @param array $parameters Query array.
*
* @return array
*/
protected static function call( $parameters = array() ) {
// define url.
$url = self::API_URL;
// add every parameter to the url.
foreach ( $parameters as $key => $value ) {
$value = sanitize_text_field( urlencode( $value ) );
$key = sanitize_text_field( $key );
$url = add_query_arg( $key, $value, $url );
}
$api_key = ( '' !== mc_get_option( 'gmap_api_key', '' ) ) ? mc_get_option( 'gmap_api_key' ) : false;
if ( ! $api_key ) {
return array();
}
$url = add_query_arg( 'key', sanitize_text_field( $api_key ), $url );
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) ) {
return array();
}
$data = $response['body'];
// redefine response as json decoded.
$response = json_decode( $data );
// return the content.
return $response->results;
}
/**
* Get address using latitude/longitude
*
* @param float $latitude Latitude.
* @param float $longitude Longitude.
*
* @return array (label, components)
*/
public static function get_address( $latitude, $longitude ) {
$address_suggestions = self::get_addresses( $latitude, $longitude );
return $address_suggestions[0];
}
/**
* Get possible addresses using latitude/longitude
*
* @param float $latitude Latitude.
* @param float $longitude Longitude.
*
* @return array(label, street, streetNumber, city, cityLocal, zip, country, countryLabel)
*/
public static function get_addresses( $latitude, $longitude ) {
// init results.
$addresses = array();
// define result.
$address_suggestions = self::call(
array(
'latlng' => $latitude . ',' . $longitude,
'sensor' => 'false',
)
);
if ( empty( $address_suggestions ) ) {
return $addresses;
}
// loop addresses.
foreach ( $address_suggestions as $key => $address_suggestion ) {
// init address.
$address = array();
// define label.
$address['label'] = isset( $address_suggestion->formatted_address ) ?
$address_suggestion->formatted_address : null;
// define address components by looping all address components.
foreach ( $address_suggestion->address_components as $component ) {
$type = $component->types[0];
$address['components'][ $type ] = array(
'long_name' => $component->long_name,
'short_name' => $component->short_name,
'types' => $component->types,
);
}
$addresses[ $key ] = $address;
}
return $addresses;
}
/**
* Get coordinates latitude/longitude
*
* @param string $street Street address.
* @param string $street_number Additional street address.
* @param string $city City.
* @param string $zip Zip.
* @param string $country Country.
*
* @return array The latitude/longitude coordinates
*/
public static function get_coordinates(
$street = null,
$street_number = null,
$city = null,
$zip = null,
$country = null
) {
// init item.
$item = array();
// add street.
if ( ! empty( $street ) ) {
$item[] = $street;
}
// add street number.
if ( ! empty( $street_number ) ) {
$item[] = $street_number;
}
// add city.
if ( ! empty( $city ) ) {
$item[] = $city;
}
// add zip.
if ( ! empty( $zip ) ) {
$item[] = $zip;
}
// add country.
if ( ! empty( $country ) ) {
$item[] = $country;
}
// define value.
$address = implode( ' ', $item );
// define result.
$results = self::call(
array(
'address' => $address,
'sensor' => 'false',
)
);
if ( empty( $results ) ) {
return array();
}
// return coordinates latitude/longitude.
return array(
'latitude' => array_key_exists( 0, $results ) ? (float) $results[0]->geometry->location->lat : null,
'longitude' => array_key_exists( 0, $results ) ? (float) $results[0]->geometry->location->lng : null,
);
}
}
/**
* Geolocation Exception
*
* @author Jeroen Desloovere <info@jeroendesloovere.be>
*/
class GeolocationException extends \Exception {}