-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_add_to_lat_lon.php
80 lines (50 loc) · 1.36 KB
/
convert_add_to_lat_lon.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
79
80
<?php
include("dbaseconnection.php");
/*
* Given an address, return the longitude and latitude using The Google Geocoding API V3
*
*/
function Get_LatLng_From_Google_Maps($address) {
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&sensor=false';
// Make the HTTP request
$data = @file_get_contents($url);
// Parse the json response
$jsondata = json_decode($data,true);
// If the json data is invalid, return empty array
if (!check_status($jsondata)) return array();
$LatLng = array(
'lat' => $jsondata["results"][0]["geometry"]["location"]["lat"],
'lng' => $jsondata["results"][0]["geometry"]["location"]["lng"],
);
return $LatLng;
}
echo"starting";
$sql="Select id,address from labbranch";
$result=mysql_query($sql);
while($row=mysql_fetch_row($result))
{$add=$row[1];
$aid=$row[0];
$add = str_replace(' ', '+', $add);
$coordinates=Get_LatLng_From_Google_Maps($add);
$lati=$coordinates['lat'];
$long=$coordinates['lng'];
$s=mysql_query("UPDATE labbranch SET latitude='$lati', longitude='$long' WHERE id='$aid'");
echo'success'.$aid.'';
}
echo"all done success";
/*
* Check if the json data from Google Geo is valid
*/
function check_status($jsondata) {
if ($jsondata["status"] == "OK") return true;
return false;
}
/*
* Print an array
*/
function d($a) {
echo "<pre>";
print_r($a);
echo "</pre>";
}
?>