PHP Get Latitude and Longitude From Address

Feb 08, 2022 . Admin

Hello Friends,

I am going to explain you example of how to PHP get latitude and longitude from address. You will learn get latitude and longitude from address using PHP. In side this article we will see the get latitude and longitude from address google map api in PHP.

This article will give you simple example of how to get latitude and longitude from address. We will use get latitude and longitude from address api.

Here i will give you many example of how to get latitude and longitude from address in PHP.

Exaple : 1
index.php
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>php Get Latitude and Longitude from Address</title>
  </head>
  <body>

    <h2>php Get Latitude and Longitude from Address</h2>

        <form action="" method="post">

            <label>Origin:</label> <input type="text" name="o" placeholder="Enter Origin location" required> <br><br>
            <label>Destination:</label> <input type="text" name="d" placeholder="Enter Destination location" required> <br><br>
            <input type="submit" value="Calculate distance & time" name="submit"> <br><br>

        </form>

        <?php
            if(isset($_POST['submit'])){
            $origin = $_POST['o']; $destination = $_POST['d'];
            $api = file_get_contents("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=".$origin."&destinations=".$destination."&key=YOUR_API_KEY");
            $data = json_decode($api);
        ?>

            <label><b>Distance: </b></label> <span><?php echo ((int)$data->rows[0]->elements[0]->distance->value / 1000).' Km'; ?></span> <br><br>
            <label><b>Travel Time: </b></label> <span><?php echo $data->rows[0]->elements[0]->duration->text; ?></span> 

        <?php } ?>  
    </body>
</html>
Output:

Exaple : 2 index.php
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>PHP Get Latitude and Longitude from Address</title>
    </head>
    <body>
        <h2>PHP Get Latitude and Longitude from Address</h2>
        <form method="post">
            <input type="text" name="address">
            <input type="submit" name="submit" value="submit">
        </form>
</html>
    <?php
    if(isset($_POST['submit']))
    {
        function getLatLong($address){
            if(!empty($address)){
                //Formatted address
                $formattedAddr = str_replace(' ','+',$address);
                //Send request and receive json data by address

                $geocodeFromAddr = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$formattedAddr.'&sensor=false
                    ®ion=india');

                $output = json_decode($geocodeFromAddr);
                //Get latitude and longitute from json data
                $data['latitude'] = $output->results[0]->geometry->location->lat;
                $data['longitude'] = $output->results[0]->geometry->location->lng;
                //Return latitude and longitude of the given address
                if(!empty($data)){
                    return $data;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
        $address = $_POST['address'];
        $latLong = getLatLong($address);
        $latitude = $latLong['latitude']?$latLong['latitude']:'Not found';
        $longitude = $latLong['longitude']?$latLong['longitude']:'Not found';
        echo "Latitude:".$latitude."<br>";
        echo "longitude:".$longitude."";
    }
    ?>
    </body>
</html>
Output:

It will help you...
#PHP