简体   繁体   中英

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM