3

PHP2 地点間の距離を計算する関数があります。

PHPコードは次のとおりです。

function distance($lat1, $lon1, $lat2, $lon2) {
   $earth_radius = 6371; 
   $delta_lat = $lat2 - $lat1 ;
   $delta_lon = $lon2 - $lon1 ;

  $distance  = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($delta_lon)) ;
  $distance  = acos($distance);
  $distance  = rad2deg($distance);
  $distance  = $distance * 60 * 1.1515;
  $distance  = round($distance, 4);
  return $distance = $distance * 1.609344;
 }

の周りで良い計算ができます25 kmが、周り500 kmの計算は間違っています。私の他の質問はit really giving miles or kilometers?

たとえば、このマップ443 kmはからMorbiまでの距離をSurat示しますが、関数は次の結果を返します274 km

4

1 に答える 1

1

わかりました。これで、道路の距離が必要であることは明らかです。適切な回答を提供できます。結果を得るには、Google API 距離マトリックスを使用できます。さまざまなオプションとパラメーターがあり、どれが自分に適しているかを判断する必要があります。ほとんどの場合、いくつかの制限があることに注意してください (無料版の場合):

<<
The Distance Matrix API has the following limits in place:
100 elements per query.
100 elements per 10 seconds.
2 500 elements per 24 hour period.
>>

ただし、質問に答える目的で、XML ファイルを取得し、SimpleXMLElement で距離/期間を解析する単純な PHP スクリプトを作成しました ...

<?php
$start = "morbi";   // You can either set lon and lat separated with a comma ex: 22.814162,70.834351
$destination = "surat";
$mode = "driving";  // Different modes: driving,walking,bicycling (be aware that some of the modes aren't available for some poi's ...)

$xml = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/xml?origins=$start&destinations=$destination&mode=$mode&language=en-EN&sensor=false");
$data = new SimpleXMLElement($xml);
$distance = $data->row->element->distance->text;
$time = $data->row->element->duration->text;
if(empty($distance) OR empty($time)){
    echo "Oops, this mode ($mode) isn't available ...";
}else{
    echo "The distance between $start and $destination is $distance and the travel duration while $mode is $time .";
}
?>

これが役に立ったことを願っています:)

于 2012-06-07T07:58:24.177 に答える