-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeocode.class.php
More file actions
222 lines (206 loc) · 6.86 KB
/
geocode.class.php
File metadata and controls
222 lines (206 loc) · 6.86 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
* geocode.class.php
*
* Copyright 2011 Wouter Snels <info@ofloo.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* GIT: https://github.com/Ofloo/geocode.php.git
*
*/
//todo http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=ulica%20nr%201%202,Warsaw,pl
class geocode {
private $_URL = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=";
private $_LAT;
private $_LNG;
private $_ACCURACY;
private $_PARTIAL = false;
private $_STATUS = -4;
private $_FORMAT;
private $_STREET;
private $_NUMBER;
private $_POSTAL;
private $_PRECISSION = 0;
/* bool returns true or false, true on result false on no result
*/
public function __construct ($address, $city, $country) {
$url = $this->_URL . urlencode ($address) . "," . urlencode ($city) . "," . urlencode ($country);
if ($xml = @simplexml_load_file($url)) {
switch ($xml->status) {
case "OK":
$this->_LAT = $xml->result->geometry->location->lat;
$this->_LNG = $xml->result->geometry->location->lng;
$this->_ACCURACY = $xml->result->geometry->location_type;
if ($xml->result->partial_match) {
$this->_PARTIAL = true;
}
if (!is_array ($xml->result->type)) {
switch ($xml->result->type) {
case "post_box":
case "floor":
case "room":
$this->_PRECISSION = 3;
break;
case "street_number":
$this->_PRECISSION = 2;
break;
case "street_address":
$this->_PRECISSION = 1;
break;
default :
$this->_PRECISSION = 0;
break;
}
} else {
$this->_PRECISSION = -1;
}
$this->_FORMAT = $xml->result->formatted_address;
foreach ($xml->result->address_component as $_list) {
switch ($_list->type) {
case "street_number":
$this->_NUMBER = $_list->long_name;
break;
case "postal_code":
$this->_POSTAL = $_list->long_name;
break;
case "route":
$this->_STREET = $_list->long_name;
break;
default:
break;
}
}
$this->_STATUS = 1;
break;
case "ZERO_RESULTS":
$this->_STATUS = 0;
break;
case "OVER_QUERY_LIMIT":
$this->_STATUS = -1;
break;
case "REQUEST_DENIED":
$this->_STATUS = -2;
break;
case "INVALID_REQUEST":
$this->_STATUS = -3;
break;
default:
$this->_STATUS = -4;
break;
}
return true;
}
return false;
}
/* double returns latitude
*/
public function lat () {
return $this->_LAT;
}
/* double returns longtitude
*/
public function lng () {
return $this->_LNG;
}
/* string returns streetname
*/
public function street () {
return $this->_STREET;
}
/* string returns street number
*/
public function number () {
return $this->_NUMBER;
}
public function postal () {
return $this->_POSTAL;
}
/* string returns postal code
*/
public function format () {
return $this->_FORMAT;
}
/* int returns precission -1 to 3
* -1: multiple results
* 0: from route level to up (default)
* 1: street address
* 2: street number
* 3: room, floor, post box
*/
public function precission () {
return $this->_PRECISSION;
}
/* bool return true on partialmatch otherwise false
*/
public function partial () {
return $this->_PARTIAL;
}
/* int returns range from 2 to -2, ..
* 2: indicates that the returned result is a precise geocode for which we have location information accurate down to street address precision.
* 1: indicates that the returned result reflects an approximation (usually on a road) interpolated between two precise points (such as intersections). Interpolated results are generally returned when rooftop geocodes are unavailable for a street address.
* 0: default
* -1: indicates that the returned result is the geometric center of a result such as a polyline (for example, a street) or polygon (region).
* -2: indicates that the returned result is approximate.
*/
public function accuracy () {
switch ($this->_ACCURACY) {
case "ROOFTOP":
return 2;
break;
case "RANGE_INTERPOLATED":
return 1;
break;
case "GEOMETRIC_CENTER":
return -1;
break;
case "APPROXIMATE":
return -2;
break;
default:
return 0;
break;
}
}
/* int returns range from 1 to -4
* 1: indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.
* 0: indicates that the geocode was successful but returned no results. This may occur if the geocode was passed a non-existent address or a latlng in a remote location.
* -1: indicates that you are over your quota.
* -2: indicates that your request was denied, generally because of lack of a sensor parameter.
* -3: generally indicates that the query (address or latlng) is missing.
* -4: default, either no request was made, or it was unsuccessfully made
*/
public function status () {
return $this->_STATUS;
}
public function __destruct () {
unset ($this->_URL);
unset ($this->_LAT);
unset ($this->_LNG);
unset ($this->_PARTIAL);
unset ($this->_ACCURACY);
unset ($this->_STATUS);
unset ($this->_FORMAT);
unset ($this->_STREET);
unset ($this->_NUMBER);
unset ($this->_POSTAL);
unset ($this->_PRECISSION);
foreach(get_object_vars($this) as $k => $v) {
unset($this->$k);
}
}
}
?>