Google Maps API

What is the quickest way to determine the distance between UK post codes as an array of data.
I am running a loop of individual requests (about 200) but it slows the page down.

I want a single destination and an array of destinations ideally.

$result = array();
$url = “https://maps.googleapis.com/maps/api/distancematrix/json?key=$api_key&origins=$zip1&destinations=$zip2&mode=$commute_mode&language=en-GB&sensor=false”;

$data = @file_get_contents($url);
$result = json_decode($data, true);

$distance = $result[‘rows’][0][‘elements’][0][‘distance’][‘value’];

Have you looked at the documentation - https://developers.google.com/maps/documentation/distance-matrix/overview and https://developers.google.com/maps/documentation/distance-matrix/distance-matrix#optional-parameters

The origins/destinations value can be a | (pipe) delimited list of places.

If you just want an “as the crow flies” distance, it’s pretty easy to just use Pythagoras to calculate it. If you’re using the Royal Mail PAF database for your postcodes, each postcode contains grid-easting and grid nothing positions that you can use. That would be much quicker because it’s not calculating a route, but obviously less accurate.

1 Like