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
    • 166
    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.

    <?php
    ini&#95;set('error&#95;reporting', E&#95;STRICT|E&#95;ALL);
    ini&#95;set('display&#95;errors', 1);
    
    $address1 = "shastri nagar Dehradun";
    $region = "India";
    $address = str&#95;replace(' ','+',$address1);
    
    $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region";
    
    $ch = curl&#95;init();
    
    curl&#95;setopt($ch, CURLOPT&#95;URL, $url);
    curl&#95;setopt($ch, CURLOPT&#95;RETURNTRANSFER, 1);
    curl&#95;setopt($ch, CURLOPT&#95;PROXYPORT, 3128);
    curl&#95;setopt($ch, CURLOPT&#95;SSL&#95;VERIFYHOST, 0);
    curl&#95;setopt($ch, CURLOPT&#95;SSL&#95;VERIFYPEER, 0);
    $response = curl&#95;exec($ch);
    curl&#95;close($ch);
    
    $response&#95;a = json&#95;decode($response);
     $lat1 = $response&#95;a->results[0]->geometry->location->lat;
     $long1 = $response&#95;a->results[0]->geometry->location->lng;
    

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

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

    Step 3: Find the distance between two places.

    $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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: