简体   繁体   English

如何使用Google地图获取距离?

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

If you try to get the distance of: 如果您尝试获得以下距离:

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

with this URL we get distance as 3.4953372691389 miles 使用此URL,我们得到的距离为3.4953372691389英里

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

in maps.google.com you get: maps.google.com您将获得:

N Main Road 8.8 mi

This distance is by road so the distance is too large. 该距离是通过公路的,因此距离太大。

Is there there any API/Any CURL request that will give me the same distance as that of maps.google.com ? 是否有API / Any CURL请求可以给我与maps.google.com相同的距离?

try this 尝试这个

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);
 }

Or use 或使用

Make sure you included to your head section. 确保您包括在头部。

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

and the call will be 来电会是

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

Refrence 提神

Look at the example here . 这里例子 All you need to do is request that URL and run the results through json_decode() . 您需要做的就是请求该URL并通过json_decode()运行结果。 Something like: 就像是:

$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