Convert an address to its Long, Lat:
First 23 lines of code will convert your address to its latitude and longitude.
Convert Long, Lat to its according Address:
Lines from 24 to 43 of below code will convert your latitude and longitude its address.Calculate Distance /between two Long, Lat:
Lines from 87 to onward of below code will calculate the distance between two given addressess.NOTE: In case of any error, feel free to ask in comments.
1: <?php
2: $postcode1 = urlencode("bb9 5sh"); // post code to look up in this case status however can easily be retrieved from a database or a form post
3: $request_url1 = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$postcode1."&sensor=true"; // the request URL you'll send to google to get back your XML feed
4: $xml1 = simplexml_load_file($request_url1) or die("url not loading");// XML request
5: $status1 = $xml1->status;// GET the request status as google's api can return several responses
6: if ($status1=="OK") {
7: //request returned completed time to get lat / lang for storage
8: $lat1 = $xml1->result->geometry->location->lat;
9: $long1 = $xml1->result->geometry->location->lng;
10: echo "$lat1,$long1"; //spit out results or you can store them in a DB if you wish
11: }
12: if ($status1=="ZERO_RESULTS") {
13: //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.
14: }
15: if ($status1=="OVER_QUERY_LIMIT") {
16: //indicates that you are over your quota of geocode requests against the google api
17: }
18: if ($status1=="REQUEST_DENIED") {
19: //indicates that your request was denied, generally because of lack of a sensor parameter.
20: }
21: if ($status1=="INVALID_REQUEST") {
22: //generally indicates that the query (address or latlng) is missing.
23: }
24: function getaddress1($lat1,$long1)
25: {
26: $url1 = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat1).','.trim($long1).'&sensor=false';
27: $json1 = @file_get_contents($url1);
28: $data1=json_decode($json1);
29: $status1 = $data1->status;
30: if($status1=="OK")
31: return $data1->results[0]->formatted_address;
32: else
33: return false;
34: }
35: $address1= getaddress1($lat1,$long1);
36: if($address1)
37: {
38: echo $address1;
39: }
40: else
41: {
42: echo "Not found";
43: }
44: echo "<br/>";
45: $postcode = urlencode("ab13"); // post code to look up in this case status however can easily be retrieved from a database or a form post
46: $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$postcode."&sensor=true"; // the request URL you'll send to google to get back your XML feed
47: $xml = simplexml_load_file($request_url) or die("url not loading");// XML request
48: $status = $xml->status;// GET the request status as google's api can return several responses
49: if ($status=="OK") {
50: //request returned completed time to get lat / lang for storage
51: $lat = $xml->result->geometry->location->lat;
52: $long = $xml->result->geometry->location->lng;
53: echo "$lat,$long"; //spit out results or you can store them in a DB if you wish
54: }
55: if ($status=="ZERO_RESULTS") {
56: //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.
57: }
58: if ($status=="OVER_QUERY_LIMIT") {
59: //indicates that you are over your quota of geocode requests against the google api
60: }
61: if ($status=="REQUEST_DENIED") {
62: //indicates that your request was denied, generally because of lack of a sensor parameter.
63: }
64: if ($status=="INVALID_REQUEST") {
65: //generally indicates that the query (address or latlng) is missing.
66: }
67: function getaddress($lat,$long)
68: {
69: $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($long).'&sensor=false';
70: $json = @file_get_contents($url);
71: $data=json_decode($json);
72: $status = $data->status;
73: if($status=="OK")
74: return $data->results[0]->formatted_address;
75: else
76: return false;
77: }
78: $address= getaddress($lat,$long);
79: if($address)
80: {
81: echo $address;
82: }
83: else
84: {
85: echo "Not found";
86: }
87: /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
88: /*:: :*/
89: /*:: This routine calculates the distance between two points (given the :*/
90: /*:: latitude/longitude of those points). It is being used to calculate :*/
91: /*:: the distance between two locations using GeoDataSource(TM) Products :*/
92: /*:: :*/
93: /*:: Definitions: :*/
94: /*:: South latitudes are negative, east longitudes are positive :*/
95: /*:: :*/
96: /*:: Passed to function: :*/
97: /*:: lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees) :*/
98: /*:: lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees) :*/
99: /*:: unit = the unit you desire for results :*/
100: /*:: where: 'M' is statute miles :*/
101: /*:: 'K' is kilometers (default) :*/
102: /*:: 'N' is nautical miles :*/
103: /*:: :*/
104: /*:: :*/
105: /*:: :*/
106: /*:: For enquiries, please comment below :*/
107: /*:: :*/
108: /*:: Ahmed Developer :*/
109: /*:: :*/
110: /*:: :*/
111: /*:: :*/
112: /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
113: function distance($lat3, $lon3, $lat4, $lon4, $unit) {
114: $theta = $lon3 - $lon4;
115: $dist = sin(deg2rad($lat3)) * sin(deg2rad($lat4)) + cos(deg2rad($lat3)) * cos(deg2rad($lat4)) * cos(deg2rad($theta));
116: $dist = acos($dist);
117: $dist = rad2deg($dist);
118: $miles = $dist * 60 * 1.1515;
119: $unit = strtoupper($unit);
120: if ($unit == "K") {
121: return ($miles * 1.609344);
122: } else if ($unit == "N") {
123: return ($miles * 0.8684);
124: } else {
125: return $miles;
126: }
127: }
128: echo "<br/>";
129: $mm=distance("$lat1", "$long1", "$lat", "$long", "M");
130: echo number_format((float)$mm, 2, '.', ''). " Miles<br>";
131: //echo distance("$lat1", "$long1", "$lat", "$long", "K") . " Kilometers<br>";
132: //echo distance("$lat1", "$long1", "$lat", "$long", "N") . " Nautical Miles<br>";
133: ?>
0 comments:
Post a Comment
Hi to all Visitors,
Feel free to Comment Here.
Your Feedback or suggestion will be appreciated.