-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmaps-addr-latlong.php
executable file
·78 lines (64 loc) · 1.64 KB
/
gmaps-addr-latlong.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
76
77
78
#!/usr/local/bin/php
<?php
//
// Author: Jon Nistor <[email protected]>
// Purpose: Return GEO Lat/Long coordinates for an address.
// Using Google Maps API / JSON responses
//
// Version: 0.01
//
// 0.01 2017-05-02 Initial
//
$o_debug = FALSE;
if( (PHP_SAPI == 'cli') && (count($argc) == 0) ) _usage($argv[0]);
if( PHP_SAPI == 'cli' )
{
$i = 0;
$address = NULL;
if( isset($argv[1]) )
{
foreach( $argv as $arg )
{
$i++;
if( $i == 1 ) continue;
$address = $address . " " . $arg;
}
} else {
$address = 0;
}
} else {
$address = $_GET['address'];
$address = str_replace("/^ /","", $address);
}
if( empty($address) || is_null($address) || (strlen($address) < 2) )
{
_usage($argv[0]);
}
// PROG: Process address.
echo "ADDR: $address\n";
$addrClean = str_replace(' ','+',$address);
if( $o_debug ) echo " DBG: Address to post to GMAPS API: $addrClean\n";
$u_opts = array('http' =>
array('timeout' => 5)
);
$u_stream = stream_context_create($u_opts);
$geocode = @file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$addrClean.'&sensor=false',false,$u_stream);
if( empty($geocode) || ! isset($geocode) )
{
echo "PULL: Failed to get GEO location, please try again.\n";
exit;
}
if( $o_debug ) print_r($geocode);
$output = json_decode($geocode);
$lat = isset($output->results[0]->geometry->location->lat) ?
$output->results[0]->geometry->location->lat : 0;
$long = isset($output->results[0]->geometry->location->lng) ?
$output->results[0]->geometry->location->lng : 0;
echo " Lat: $lat\n";
echo "Long: $long\n";
function _usage($script)
{
echo "Usage: $script 'address, city, country'\n";
exit(1);
}
?>