PluginProbe
Leaflet Map / 2.16.0
Leaflet Map v2.16.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.16.0, at class.geocoder.php

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