PHP and Google maps - distance between post codes

Hi,
I’m writing a system for a charity which requires us to cost journeys. I could sit in front of the computer, and type pairs of postcodes to get the information, but doing it this way is far more interesting.
Here is my code so far:

<?php
  $endpoint = 'http://maps.google.com/maps/api/directions/json?';
  $params = array(
    "origin" => "SL8 5JU",
        'destination' => "HP13 5RD",
    'mode' => 'driving',
    'sensor' => 'false',
  );
  $json = file_get_contents($endpoint . http_build_query($params));
  $data = json_decode($json);
  if ($data->status === 'OK') {
    print_r($data);
    $route = $data->routes[0];
    // Get the distance.
    $distance = $route->legs[0]->distance->text;
    // Break the distance into value/unit variables
    list($miles, $unit) = explode(' ', $distance);
    // Remove any commas.
    $miles = str_replace(',', '', $miles);
    
    // Cast the value as a floating point number to do any comparisons.
    if ((float)$miles > 2250) {
      print 'TRUE';
    }
  }

I got an API key from Google maps, but not entirely sure what to do with it.
Thanks.

$params needs an entry for key.

(Based on the example on Directions API overview | Google for Developers)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.