-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geocoder.php
72 lines (56 loc) · 1.93 KB
/
Geocoder.php
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
<?php
class Geocoder {
public function Geocode($address) {
//CODE FROM GOOGLE
//---------------------------------------------------------
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://maps.google.com/maps/geo?output=xml";
// Iterate through the rows, geocoding each address
$geocode_pending = true;
while ($geocode_pending) {
$request_url = $base_url . "&q=" . urlencode($address);
$xml = simplexml_load_file($request_url);
if (!$xml){
FormResponse::status_message(sprintf($request_url),'bad');
return FormResponse::respond();
}
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
// Successful geocode
$geocode_pending = false;
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
//$this->Lat = $lat;
//$this->Lon = $lng;
$LatLon = array(
"Lat" => $lat,
"Lon" => $lng
);
return $LatLon;
} else if (strcmp($status, "620") == 0) {
// sent geocodes too fast
$delay += 100000;
} else {
// failure to geocode
$geocode_pending = false;
$errorMes = "Address " . $address . " failed to geocoded. Received status " . $status . "\n";
FormResponse::status_message(sprintf('Invalid query'),'bad');
$From = "[email protected]";
$To = Email::getAdminEmail();
$Subject = "GeoCoder Error";
$body = $errorMes . "Address " . $address . " failed to geocoded. Received status " . $status . "\n";
$email = new Email($From, $To, $Subject, $body);
//send mail
$email->sendPlain();
return FormResponse::respond();
echo "Address " . $address . " failed to geocoded. ";
echo "Received status " . $status . "\n";
}
usleep($delay);
}
}
}