-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lat_Lng_Search.php
75 lines (57 loc) · 1.45 KB
/
Lat_Lng_Search.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
73
74
75
<?php
/**
* Library class Lat_Lng_Search
*
* Leverages Googlemap API to pull back a (WGS84?) lat long pair. Requires CURL
*
* @author Ciaran Bradley
*
* @usage $lat_lng_array = Lat_Lng_Search::lookup_address("1 The North Pole, North Pole");
*/
class Lat_Lng_Search
{
/**
* Performs the search using google api
*
* @param $address string
* @return $result or false (result is array('lat' => float, 'lng' => float)
*/
public static function lookup_address($address)
{
$url = 'http://maps.google.com/maps/api/geocode/xml?address=' . urlencode($address) . '&sensor=true';
if(in_array('curl', get_loaded_extensions()))
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
}
else
{
$handle = fopen($url, "rb");
$response = '';
while(!feof($handle))
{
$response .= fread($handle, 1024);
}
fclose($handle);
}
$xml = new SimpleXMLElement($response);
$status = exist($xml->status);
if($status == 'OK')
{
$lat = (string) $xml->result->geometry->location->lat;
$lng = (string) $xml->result->geometry->location->lng;
if($lat && $lng && trim($lat) != "" && trim($lng) != "")
{
$result = array(
'lat' => $lat,
'lng' => $lng,
);
return $result;
}
}
return false; // If you get here, it's all gone wrong
}
}// End Lat_Lng_Search
?>