Created
March 15, 2016 07:51
-
-
Save GiovanniK/012953355ccdbcec7994 to your computer and use it in GitHub Desktop.
Google maps - duratie en afstand berekenen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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