-1

I have this webpage that takes two postcodes and gets the distance apart along with other things that Google spews back.

The code:

$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$postcode1&destinations=$postcode2&mode=bicycling&language=en-EN&sensor=false";
$data = @file_get_contents($url);

$result = json_decode($data, true);

print_r($result);

This is the result:

{
   "destination_addresses" : [ "Pound Rd, Oldbury B68 8NE, UK" ],
   "origin_addresses" : [ "Vyse St, Birmingham B18 6NE, UK" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "10.4 km",
                  "value" : 10384
               },
               "duration" : {
                  "text" : "36 mins",
                  "value" : 2133
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

I'm new to JSON myself so excuse me if this sounds stupid. I've tried quite a bit. The closest I've got is

echo $result['destination_addresses'][0];

which returns the destination address. Id like to know how to get everything individually especially the distance text.

Thanks in advance!

Jake32

Jake32
  • 21
  • 4

1 Answers1

1

Below is the solution :-

echo "Destination Address=".$result['destination_addresses'][0]."<br>";
echo "Origin Address=".$result['origin_addresses'][0]."<br>";
echo "Distance text=".$result['rows'][0]['elements'][0]['distance']['text']."<br>";
Webdev
  • 617
  • 6
  • 24