php - Get driving distance between two points using Google Maps API and calculate time -
how display time example 16:00 have idea how make this?
function getdrivingdistance($lat1, $lat2, $long1, $long2) { $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=de"; $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_proxyport, 3128); curl_setopt($ch, curlopt_ssl_verifyhost, 0); curl_setopt($ch, curlopt_ssl_verifypeer, 0); $response = curl_exec($ch); curl_close($ch); $response_a = json_decode($response, true); $dist = $response_a['rows'][0]['elements'][0]['distance']['text']; $time = $response_a['rows'][0]['elements'][0]['duration']['text']; return array('distance' => $dist, 'time' => $time); } //$pickuplocation = 'herbststrasse 5'; //$droplocation = 'pastorstrasse 5'; $coordinates1 = get_coordinates('wien', 'stefan esders platz 1', ''); $coordinates2 = get_coordinates('wien', 'pastorstrasse 1', ''); if ( !$coordinates1 || !$coordinates2 ) { echo 'bad address.'; } else { $dist = getdrivingdistance($coordinates1['lat'], $coordinates2['lat'], $coordinates1['long'], $coordinates2['long']); echo 'entfernung: <b>'.$dist['distance'].'</b><br>fahrzeit: <b>'.$dist['time'].'</b>'; } ?>
entfernung: 662 km fahrzeit: 6 stunden, 56 minuten
i need display time example 6:56 can help?
instead of getting 'text' field response:
$dist = $response_a['rows'][0]['elements'][0]['distance']['text']; $time = $response_a['rows'][0]['elements'][0]['duration']['text'];
try value:
$dist = $response_a['rows'][0]['elements'][0]['distance']['text']; $time = $response_a['rows'][0]['elements'][0]['duration']['value'];
that total number of seconds, math on , can format please.
$hours = floor($time / 3600); $mins = floor($time / 60 % 60); $secs = floor($time % 60);
Comments
Post a Comment