PluginProbe
Leaflet Map / 2.10.0
Leaflet Map v2.10.0
3.4.7 3.4.6 trunk 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.11.5 2.12.0 2.13.0 2.14.0 2.15.0 2.16.0 2.16.1 2.16.2 2.17.0 2.17.1 2.17.2 2.17.3 2.18.0 2.19.0 2.19.1 All 49 releases
leaflet-map / class.geocoder.php

class.geocoder.php in Leaflet Map 2.10.0, at class.geocoder.php

195 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Geocoder
4 *
5 * calls the specific geocoder function (chosen in admin or default: google_geocode)
6 *
7 */
8
9 class Leaflet_Geocoder {
10 /**
11 * Geocoder should return this on error/not found
12 * @var array $not_found
13 */
14 private $not_found = array('lat' => 0, 'lng' => 0);
15 /**
16 * Latitude
17 * @var float $lat
18 */
19 public $lat = 0;
20 /**
21 * Longitude
22 * @var float $lng
23 */
24 public $lng = 0;
25
26 /**
27 * new Geocoder from address
28 *
29 * handles url encoding and caching
30 *
31 * @param string $address the requested address to look up
32 * @return NOTHING
33 */
34 public function __construct ($address) {
35 $settings = Leaflet_Map_Plugin_Settings::init();
36 $address = urlencode( $address );
37
38 $geocoder = $settings->get('geocoder');
39
40 $cached_address = 'leaflet_' . $geocoder . '_' . $address;
41
42 /* retrieve cached geocoded location */
43 $found_cache = get_option( $cached_address );
44
45 if ( $found_cache ) {
46 $location = $found_cache;
47 } else {
48 // try geocoding
49 $geocoding_method = $geocoder . '_geocode';
50
51 try {
52 $location = (Object) $this->$geocoding_method( $address );
53
54 /* add location */
55 add_option($cached_address, $location);
56
57 /* add option key to locations for clean up purposes */
58 $locations = get_option('leaflet_geocoded_locations', array());
59 array_push($locations, $cached_address);
60 update_option('leaflet_geocoded_locations', $locations);
61 } catch (Exception $e) {
62 // failed
63 $location = $this->not_found;
64 }
65 }
66
67 // set variables
68 $this->lat = $location->lat;
69 $this->lng = $location->lng;
70 }
71
72 /**
73 * Removes location caches
74 */
75 public static function remove_caches () {
76 $addresses = get_option('leaflet_geocoded_locations', array());
77 foreach ($addresses as $address) {
78 delete_option($address);
79 }
80 delete_option('leaflet_geocoded_locations');
81 }
82
83 /**
84 * Used by geocoders to make requests via curl or file_get_contents
85 *
86 * includes a try/catch
87 *
88 * @param string $url the urlencoded request url
89 * @return varies object from API or null (failed)
90 */
91 private function get_url( $url ) {
92 if (in_array('curl', get_loaded_extensions())) {
93 /* try curl */
94 $ch = curl_init();
95
96 curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
97 curl_setopt($ch, CURLOPT_HEADER, 0);
98 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
99 curl_setopt($ch, CURLOPT_URL, $url);
100 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
101
102 $data = curl_exec($ch);
103 curl_close($ch);
104
105 return $data;
106 } else if (ini_get('allow_url_fopen')) {
107 /* try file get contents */
108 return file_get_contents( $url );
109 }
110
111 $error_msg = 'Could not get url: ' . $url;
112 throw new Exception( $error_msg );
113 }
114
115 /**
116 * Google geocoder (https://developers.google.com/maps/documentation/geocoding/start)
117 *
118 * @param string $address the urlencoded address to look up
119 * @return varies object from API or null (failed)
120 */
121
122 private function google_geocode ( $address ) {
123 $geocode_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=';
124 $geocode_url .= $address;
125 $json = $this->get_url($geocode_url);
126
127 if ($json) {
128 $json = json_decode($json);
129 /* found location */
130 if ($json->{'status'} == 'OK') {
131
132 $location = $json->{'results'}[0]->{'geometry'}->{'location'};
133
134 return (Object) $location;
135 }
136 }
137
138 return $this->not_found;
139 }
140
141 /**
142 * OpenStreetMap geocoder Nominatim (https://nominatim.openstreetmap.org/)
143 *
144 * @param string $address the urlencoded address to look up
145 * @return varies object from API or null (failed)
146 */
147
148 private function osm_geocode ( $address ) {
149 $geocode_url = 'https://nominatim.openstreetmap.org/?format=json&limit=1&q=';
150 $geocode_url .= $address;
151 $json = $this->get_url($geocode_url);
152 $json = json_decode($json);
153
154 if (is_array($json) &&
155 is_object($json[0]) &&
156 $json[0]->lat) {
157 // found location
158 return (Object) array(
159 'lat' => $json[0]->lat,
160 'lng' => $json[0]->lon,
161 );
162 }
163
164 return $this->not_found;
165 }
166
167 /**
168 * Danish Addresses Web Application
169 * (https://dawa.aws.dk)
170 *
171 * @param string $address the urlencoded address to look up
172 * @return varies object from API or null (failed)
173 */
174
175 private function dawa_geocode ( $address ) {
176 $geocode_url = 'https://dawa.aws.dk/adresser?format=json&q=';
177 $geocode_url .= $address;
178 $json = $this->get_url($geocode_url);
179 $json = json_decode($json);
180
181 if (is_array($json) &&
182 is_object($json[0]) &&
183 $json[0]->status == 1) {
184 /* found location */
185 $location = (Object) array(
186 'lat' => $json[0]->adgangsadresse->adgangspunkt->koordinater[1],
187 'lng' => $json[0]->adgangsadresse->adgangspunkt->koordinater[0]
188 );
189
190 return $location;
191 }
192
193 return $this->not_found;
194 }
195 }