Skip to content

Instantly share code, notes, and snippets.

@GiovanniK
Created March 15, 2016 07:51
Show Gist options
  • Save GiovanniK/012953355ccdbcec7994 to your computer and use it in GitHub Desktop.
Save GiovanniK/012953355ccdbcec7994 to your computer and use it in GitHub Desktop.
Google maps - duratie en afstand berekenen
<?php
function distance($start, $destination)
{
// Send request to Google
$start = urlencode($start);
$destination = urlencode($destination);
$url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$start&destinations=$destination&mode=driving&language=nl-NL&sensor=false";
// Get response
$response = json_decode(file_get_contents($url));
if($response->status !== 'OK') return 'ERROR';
if($response->rows{0}->elements{0}->status !== 'OK') return 'NOT_FOUND';
// Return parsed result
$result = [
'start' => str_replace(', Nederland', '', $response->origin_addresses{0}),
'destination' => str_replace(', Nederland', '', $response->destination_addresses{0}),
'info' => [
'distance' => [
'text' => $response->rows{0}->elements{0}->distance->text,
'value' => $response->rows{0}->elements{0}->distance->value,
'value_km' => $response->rows{0}->elements{0}->distance->value / 1000
],
'duration' => [
'text' => $response->rows{0}->elements{0}->duration->text,
'value' => $response->rows{0}->elements{0}->duration->value,
'value_minutes' => $response->rows{0}->elements{0}->duration->value / 60
]
]
];
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment