簡體   English   中英

如何使用Google地圖獲取距離?

[英]How can I get the distance using google maps?

如果您嘗試獲得以下距離:

Kalyani Nagar, Wadgaon Sheri, Pune, Maharashtra, India
Karve Nagar, Hingne Budrukh, Pune, Maharashtra, India

使用此URL,我們得到的距離為3.4953372691389英里

http://www.sudhirhub.com/2010/05/find-distance-between-two-cities-using.html

maps.google.com您將獲得:

N Main Road 8.8 mi

該距離是通過公路的,因此距離太大。

是否有API / Any CURL請求可以給我與maps.google.com相同的距離?

嘗試這個

rad = function(x) {return x*Math.PI/180;}

distHaversine = function(p1, p2) {
  var R = 6371; // earth's mean radius in km
  var dLat  = rad(p2.lat() - p1.lat());
  var dLong = rad(p2.lng() - p1.lng());

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
      Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) * Math.sin(dLong/2) *          Math.sin(dLong/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var d = R * c;

  return d.toFixed(3);
 }

或使用

確保您包括在頭部。

 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&v=3&libraries=geometry"></script>

來電會是

google.maps.geometry.spherical.computeDistanceBetween (latLngA, latLngB);

提神

這里例子 您需要做的就是請求該URL並通過json_decode()運行結果。 就像是:

$result = file_get_contents('http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false');
$result = json_decode($results);
$distance = $result->routes->legs->distance['text'];
echo $distance // Outputs: "583 mi"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM