Google APIを利用して、PHPで2つの住所の距離と所要時間を取得するサンプル※車での移動距離と所要時間
status;
if ( $status == 'ZERO_RESULTS' )
{
return FALSE;
}
else
{
$return = array('lat' => $response_a->results[0]->geometry->location->lat, 'long' => $long = $response_a->results[0]->geometry->location->lng);
return $return;
}
}
function GetDrivingDistance($lat1, $lat2, $long1, $long2)
{
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=pl-PL";
$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);
}
//Usage:
$coordinates1 = get_coordinates('東京都三鷹市上連雀8丁目3番3号');
$coordinates2 = get_coordinates('東京都千代田区富士見2-4-1');
if ( !$coordinates1 || !$coordinates2 )
{
echo 'Bad address.';
}
else
{
$dist = GetDrivingDistance($coordinates1['lat'], $coordinates2['lat'], $coordinates1['long'], $coordinates2['long']);
echo 'Distance: '.$dist['distance'].'
Travel time duration: '.$dist['time'].'';
}
こんな風に結果が表示されます:
Distance: 22,3 km
Travel time duration: 40 min
hello admin, i am here to say good work. you did great work and it is the best site. you can also software download free full version free here
返信削除