PluginProbe
Leaflet Map / 3.0.5
Leaflet Map v3.0.5
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 3.0.5, at class.geocoder.php

196 lines 5.8 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 // trim all quotes (even smart) from address
37 $address = trim($address, '\'"”');
38 $address = urlencode( $address );
39
40 $geocoder = $settings->get('geocoder');
41
42 $cached_address = 'leaflet_' . $geocoder . '_' . $address;
43
44 /* retrieve cached geocoded location */
45 $found_cache = get_option( $cached_address );
46
47 if ( $found_cache ) {
48 $location = $found_cache;
49 } else {
50 // try geocoding
51 $geocoding_method = $geocoder . '_geocode';
52
53 try {
54 $location = (Object) $this->$geocoding_method( $address );
55
56 /* add location */
57 add_option($cached_address, $location);
58
59 /* add option key to locations for clean up purposes */
60 $locations = get_option('leaflet_geocoded_locations', array());
61 array_push($locations, $cached_address);
62 update_option('leaflet_geocoded_locations', $locations);
63 } catch (Exception $e) {
64 // failed
65 $location = $this->not_found;
66 }
67 }
68
69 // set variables
70 $this->lat = $location->lat;
71 $this->lng = $location->lng;
72 }
73
74 /**
75 * Removes location caches
76 */
77 public static function remove_caches () {
78 $addresses = get_option('leaflet_geocoded_locations', array());
79 foreach ($addresses as $address) {
80 delete_option($address);
81 }
82 delete_option('leaflet_geocoded_locations');
83 }
84
85 /**
86 * Used by geocoders to make requests via curl or file_get_contents
87 *
88 * includes a try/catch
89 *
90 * @param string $url the urlencoded request url
91 * @return varies object from API or null (failed)
92 */
93 private function get_url( $url ) {
94 $referer = get_site_url();
95
96 if (in_array('curl', get_loaded_extensions())) {
97 /* try curl */
98 $ch = curl_init();
99
100 curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
101 curl_setopt($ch, CURLOPT_HEADER, 0);
102 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
103 curl_setopt($ch, CURLOPT_REFERER, $referer);
104 curl_setopt($ch, CURLOPT_URL, $url);
105 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
106
107 $data = curl_exec($ch);
108 curl_close($ch);
109
110 return $data;
111 } else if (ini_get('allow_url_fopen')) {
112 /* try file get contents */
113
114 $opts = array(
115 'http' => array(
116 'header' => array("Referer: $referer\r\n")
117 )
118 );
119 $context = stream_context_create($opts);
120
121 return file_get_contents($url, false, $context);
122 }
123
124 $error_msg = 'Could not get url: ' . $url;
125 throw new Exception( $error_msg );
126 }
127
128 /**
129 * Google geocoder (https://developers.google.com/maps/documentation/geocoding/start)
130 *
131 * @param string $address the urlencoded address to look up
132 * @return varies object from API or null (failed)
133 */
134
135 private function google_geocode ( $address ) {
136 // Leaflet_Map_Plugin_Settings
137 $settings = Leaflet_Map_Plugin_Settings::init();
138 $key = $settings->get('google_appkey');
139
140 $geocode_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s';
141 $geocode_url = sprintf($geocode_url, $address, $key);
142
143 $json = $this->get_url($geocode_url);
144 $json = json_decode($json);
145
146 /* found location */
147 if ($json->status == 'OK') {
148
149 $location = $json->results[0]->geometry->location;
150
151 return (Object) $location;
152 }
153
154 throw new Exception('No Address Found');
155 }
156
157 /**
158 * OpenStreetMap geocoder Nominatim (https://nominatim.openstreetmap.org/)
159 *
160 * @param string $address the urlencoded address to look up
161 * @return varies object from API or null (failed)
162 */
163
164 private function osm_geocode ( $address ) {
165 $geocode_url = 'https://nominatim.openstreetmap.org/?format=json&limit=1&q=';
166 $geocode_url .= $address;
167 $json = $this->get_url($geocode_url);
168 $json = json_decode($json);
169
170 return (Object) array(
171 'lat' => $json[0]->lat,
172 'lng' => $json[0]->lon,
173 );
174 }
175
176 /**
177 * TODO: does this still work?
178 * Danish Addresses Web Application
179 * (https://dawa.aws.dk)
180 *
181 * @param string $address the urlencoded address to look up
182 * @return varies object from API or null (failed)
183 */
184 private function dawa_geocode ( $address ) {
185 $geocode_url = 'https://dawa.aws.dk/adresser?format=json&q=';
186 $geocode_url .= $address;
187 $json = $this->get_url($geocode_url);
188 $json = json_decode($json);
189
190 /* found location */
191 return (Object) array(
192 'lat' => $json[0]->adgangsadresse->adgangspunkt->koordinater[1],
193 'lng' => $json[0]->adgangsadresse->adgangspunkt->koordinater[0]
194 );
195 }
196 }