over 10 years ago
This code will help in finding the latitude and longitude of any place using curl and php.
- <?php
- ini_set('error_reporting', E_STRICT|E_ALL);
- ini_set('display_errors', 1);
- $address1 = "shastri nagar Dehradun";
- $region = "India";
- $address = str_replace(' ','+',$address1);
- $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- $response = curl_exec($ch);
- curl_close($ch);
- $response_a = json_decode($response);
- $lat1 = $response_a->results[0]->geometry->location->lat;
- $long1 = $response_a->results[0]->geometry->location->lng;
<?php ini_set('error_reporting', E_STRICT|E_ALL); ini_set('display_errors', 1); $address1 = "shastri nagar Dehradun"; $region = "India"; $address = str_replace(' ','+',$address1); $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_PROXYPORT, 3128); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $response = curl_exec($ch); curl_close($ch); $response_a = json_decode($response); $lat1 = $response_a->results[0]->geometry->location->lat; $long1 = $response_a->results[0]->geometry->location->lng;
- $address2 = "Clock Tower Dehradun";
- $address = str_replace(' ','+',$address2);
- $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region";
- print("code sample"); $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- $response = curl_exec($ch);
- curl_close($ch);
- $response_a = json_decode($response);
- $lat2 = $response_a->results[0]->geometry->location->lat;
- $long2 = $response_a->results[0]->geometry->location->lng;
$address2 = "Clock Tower Dehradun"; $address = str_replace(' ','+',$address2); $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region"; print("code sample"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_PROXYPORT, 3128); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $response = curl_exec($ch); curl_close($ch); $response_a = json_decode($response); $lat2 = $response_a->results[0]->geometry->location->lat; $long2 = $response_a->results[0]->geometry->location->lng;
- $unit = "K";
- $difference = getDistance($lat1,$long1,$lat2,$long2,$unit);
- echo "Distance between ".$address1." "."&"." ".$address2." = ".$difference."Km";
- function getDistance($lat1, $long1, $lat2, $long2, $unit) {
- $theta = $long1 - $long2;$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
- $dist = acos($dist);
- $dist = rad2deg($dist);
- $miles = $dist * 60 * 1.1515;
- if ($unit == "K") {
- return ($miles * 1.609344);
- } else {
- return $miles;
- }
- }
- ?>
$unit = "K"; $difference = getDistance($lat1,$long1,$lat2,$long2,$unit); echo "Distance between ".$address1." "."&"." ".$address2." = ".$difference."Km"; function getDistance($lat1, $long1, $lat2, $long2, $unit) { $theta = $long1 - $long2;$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; if ($unit == "K") { return ($miles * 1.609344); } else { return $miles; } } ?>
0 Comment(s)