【问题标题】:How can I apply a functionality to find the distance between two places by Google map API?如何通过 Google 地图 API 应用功能来查找两个地点之间的距离?
【发布时间】:2014-01-16 13:10:05
【问题描述】:

我想要一个页面,在选择一个城市后,相邻的地图将指向该城市。我需要有两个带有自动建议选项的字段,这些字段将填充最相关的地方。选择这些地点后会自动计算距离。我们想为此实现一个谷歌地图API。

【问题讨论】:

    标签: google-maps google-maps-api-3 google-api-php-client


    【解决方案1】:

    发出一个 AJAX 请求,其中包含 API 找到的两个城市坐标。

    在 PHP 中,您可以计算两点之间的距离,而无需使用 Javascript 地图 api。

    如果你不使用 API 就可以得到坐标,你可以直接在 PHP 中计算...

    Javascript:

    $.get( php_script , function( data ) {
       alert( "The distance is "+data+" km" );
    });
    

    PHP:

    unset($params);
    $params["point1"]["lat"]; 
    $params["point1"]["lng"]; 
    $params["point2"]["lat"]; 
    $params["point2"]["lng"]; 
    echo orthodromy($params);
    
    function orthodromy($params) {
       unset($lat1, $lng1, $lat2, $lng2);
       $lat1 = $params["point1"]["lat"]; 
       $lng1 = $params["point1"]["lng"]; 
       $lat2 = $params["point2"]["lat"]; 
       $lng2 = $params["point2"]["lng"]; 
    
       if (!is_numeric($lat1) || !is_numeric($lng1) || !is_numeric($lat2) 
           || !is_numeric($lng2)) return false;
    
       unset($dist);
       $dist = acos(
          sin( deg2rad($lat1) ) *
          sin( deg2rad($lat2) ) +
          cos( deg2rad($lat1) ) *
          cos( deg2rad($lat2) ) *
          cos( deg2rad($lng1-$lng2) )
       ) * 6371;
       if (!is_numeric($dist)) return false; else return $dist;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 2012-07-02
      • 2011-12-20
      相关资源
      最近更新 更多