PluginProbe
Leaflet Map / 2.11.3
Leaflet Map v2.11.3
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.11.3, at class.geocoder.php

196 lines 5.0 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_REFERER, get_site_url());
100 curl_setopt($ch, CURLOPT_URL, $url);
101 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
102
103 $data = curl_exec($ch);
104 curl_close($ch);
105
106 return $data;
107 } else if (ini_get('allow_url_fopen')) {
108 /* try file get contents */
109 return file_get_contents( $url );
110 }
111
112 $error_msg = 'Could not get url: ' . $url;
113 throw new Exception( $error_msg );
114 }
115
116 /**
117 * Google geocoder (https://developers.google.com/maps/documentation/geocoding/start)
118 *
119 * @param string $address the urlencoded address to look up
120 * @return varies object from API or null (failed)
121 */
122
123 private function google_geocode ( $address ) {
124 $geocode_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=';
125 $geocode_url .= $address;
126 $json = $this->get_url($geocode_url);
127
128 if ($json) {
129 $json = json_decode($json);
130 /* found location */
131 if ($json->{'status'} == 'OK') {
132
133 $location = $json->{'results'}[0]->{'geometry'}->{'location'};
134
135 return (Object) $location;
136 }
137 }
138
139 throw new Exception('No Address Found');
140 }
141
142 /**
143 * OpenStreetMap geocoder Nominatim (https://nominatim.openstreetmap.org/)
144 *
145 * @param string $address the urlencoded address to look up
146 * @return varies object from API or null (failed)
147 */
148
149 private function osm_geocode ( $address ) {
150 $geocode_url = 'https://nominatim.openstreetmap.org/?format=json&limit=1&q=';
151 $geocode_url .= $address;
152 $json = $this->get_url($geocode_url);
153 $json = json_decode($json);
154
155 if (is_array($json) &&
156 is_object($json[0]) &&
157 $json[0]->lat) {
158 // found location
159 return (Object) array(
160 'lat' => $json[0]->lat,
161 'lng' => $json[0]->lon,
162 );
163 }
164
165 throw new Exception('No Address Found');
166 }
167
168 /**
169 * Danish Addresses Web Application
170 * (https://dawa.aws.dk)
171 *
172 * @param string $address the urlencoded address to look up
173 * @return varies object from API or null (failed)
174 */
175
176 private function dawa_geocode ( $address ) {
177 $geocode_url = 'https://dawa.aws.dk/adresser?format=json&q=';
178 $geocode_url .= $address;
179 $json = $this->get_url($geocode_url);
180 $json = json_decode($json);
181
182 if (is_array($json) &&
183 is_object($json[0]) &&
184 $json[0]->status == 1) {
185 /* found location */
186 $location = (Object) array(
187 'lat' => $json[0]->adgangsadresse->adgangspunkt->koordinater[1],
188 'lng' => $json[0]->adgangsadresse->adgangspunkt->koordinater[0]
189 );
190
191 return $location;
192 }
193
194 throw new Exception('No Address Found');
195 }
196 }