开发者

How can I get the distance using google maps?

开发者 https://www.devze.com 2023-03-24 00:40 出处:网络
If you try to get the distance of: Kalyani Nagar, Wadgaon Sheri, Pune, Maharashtra, India Karve Nagar, Hingne Budrukh, Pune, Mah开发者_StackOverflowarashtra, India

If you try to get the distance of:

Kalyani Nagar, Wadgaon Sheri, Pune, Maharashtra, India
Karve Nagar, Hingne Budrukh, Pune, Mah开发者_StackOverflowarashtra, India

with this URL we get distance as 3.4953372691389 miles

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

in maps.google.com you get:

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?


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(). 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"
0

精彩评论

暂无评论...
验证码 换一张
取 消