Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Find latitude and longitude of a place.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 203
    Comment on it

    This code will help in finding the latitude and longitude of any place using curl and php.

    Step 1: Finding the latitude and longitude for first place.

    1. <?php
    2. ini&#95;set('error&#95;reporting', E&#95;STRICT|E&#95;ALL);
    3. ini&#95;set('display&#95;errors', 1);
    4.  
    5. $address1 = "shastri nagar Dehradun";
    6. $region = "India";
    7. $address = str&#95;replace(' ','+',$address1);
    8.  
    9. $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region";
    10.  
    11. $ch = curl&#95;init();
    12.  
    13. curl&#95;setopt($ch, CURLOPT&#95;URL, $url);
    14. curl&#95;setopt($ch, CURLOPT&#95;RETURNTRANSFER, 1);
    15. curl&#95;setopt($ch, CURLOPT&#95;PROXYPORT, 3128);
    16. curl&#95;setopt($ch, CURLOPT&#95;SSL&#95;VERIFYHOST, 0);
    17. curl&#95;setopt($ch, CURLOPT&#95;SSL&#95;VERIFYPEER, 0);
    18. $response = curl&#95;exec($ch);
    19. curl&#95;close($ch);
    20.  
    21. $response&#95;a = json&#95;decode($response);
    22. $lat1 = $response&#95;a->results[0]->geometry->location->lat;
    23. $long1 = $response&#95;a->results[0]->geometry->location->lng;

    Step 2: Finding the latitude and longitude for second place.

    1. $address2 = "Clock Tower Dehradun";
    2. $address = str&#95;replace(' ','+',$address2);
    3. $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region";
    4. print("code sample"); $ch = curl&#95;init();
    5. curl&#95;setopt($ch, CURLOPT&#95;URL, $url);
    6. curl&#95;setopt($ch, CURLOPT&#95;RETURNTRANSFER, 1);
    7. curl&#95;setopt($ch, CURLOPT&#95;PROXYPORT, 3128);
    8. curl&#95;setopt($ch, CURLOPT&#95;SSL&#95;VERIFYHOST, 0);
    9. curl&#95;setopt($ch, CURLOPT&#95;SSL&#95;VERIFYPEER, 0);
    10. $response = curl&#95;exec($ch);
    11. curl&#95;close($ch);
    12. $response&#95;a = json&#95;decode($response);
    13.  
    14. $lat2 = $response&#95;a->results[0]->geometry->location->lat;
    15. $long2 = $response&#95;a->results[0]->geometry->location->lng;

    Step 3: Find the distance between two places.

    1. $unit = "K";
    2. $difference = getDistance($lat1,$long1,$lat2,$long2,$unit);
    3.  
    4. echo "Distance between ".$address1." "."&"." ".$address2." = ".$difference."Km";
    5.  
    6.  
    7. function getDistance($lat1, $long1, $lat2, $long2, $unit) {
    8. $theta = $long1 - $long2;$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    9. $dist = acos($dist);
    10. $dist = rad2deg($dist);
    11. $miles = $dist * 60 * 1.1515;
    12.  
    13.  
    14. if ($unit == "K") {
    15. return ($miles * 1.609344);
    16. } else {
    17. return $miles;
    18. }
    19. }
    20. ?>

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: