phpgoogle-mapstimegoogle-distancematrix-api

How to get distance between two locations using Google Distance Matrix


<html>
    <body>

        <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
        $con = mysqli_connect("localhost","root","","buspass");

            if(isset($_POST['submit'])){
            $origin = $_POST['o']; 
            $destination = $_POST['d'];
            $api = file_get_contents("https://www.google.com/maps/search/?api=1&query=centurylink+field/distancematrix/json?units=imperial&origins=".$origin."&destinations=".$destination."&key=API_KEY");
            $data = json_decode($api);
        ?>
                <label><b>Distance: </b></label> <span>
                <?php 
                if (is_object($data)) {
                    echo((int)$data->rows[0]->elements[0]->distance->value / 1000).' Km'; //line24
                }
                ?>
                </span> <br><br>
            <label><b>Travel Time: </b></label> <span>
            <?php
            if (is_object($data)) {
                echo $data->rows[0]->elements[0]->duration->text;        //line 31
            }
            ?>
            </span> 
    
        <?php 
        } 
        ?>

    </body>
</html>

When I enter the two locations and post, it should output the distance between the 2 locations and approximate time that will be taken to arrive.

Error Message:

Notice: Trying to get property 'rows' of non-object in C:\xampp\htdocs\Bus Pass\distance.php on line 24

Notice: Trying to get property 'elements' of non-object in C:\xampp\htdocs\Bus Pass\distance.php on line 24

Notice: Trying to get property 'distance' of non-object in C:\xampp\htdocs\Bus Pass\distance.php on line 24

Notice: Trying to get property 'value' of non-object in C:\xampp\htdocs\Bus Pass\distance.php on line 24 0 Km

Travel Time: Notice: Trying to get property 'rows' of non-object in C:\xampp\htdocs\Bus Pass\distance.php on line 31

Notice: Trying to get property 'elements' of non-object in C:\xampp\htdocs\Bus Pass\distance.php on line 31

Notice: Trying to get property 'duration' of non-object in C:\xampp\htdocs\Bus Pass\distance.php on line 31

Notice: Trying to get property 'text' of non-object in C:\xampp\htdocs\Bus Pass\distance.php on line 31


Solution

  • I had some hard time with PHP's JSON objects my self,

    1. Replace $data = json_decode($api); with $data = json_decode($api, true); to get an associative array.

    2. Remove any object checks (like if (is_object($data)) {).

    3. Finally, use array style calls instead of object style, for example, change your code from:

    $data->rows[0]->elements[0]->distance->value
    

    into something like:

    $data['rows'][0]['elements'][0]['distance']['value']