1

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?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
user827726
  • 49
  • 1
  • 5
  • http://www.freemaptools.com/how-far-is-it-between.htm , http://www.daftlogic.com/projects-google-maps-distance-calculator.htm try this – jeni Aug 01 '11 at 07:29
  • @ jeni, still it not matches with maps.google.com – user827726 Aug 01 '11 at 07:39
  • If you need a php based solution for cakephp, you might be looking for this approach: http://www.dereuromark.de/2012/06/12/geocoding-with-cakephp/ it has a lib and optionally a behavior to geocode just about anything via cakePHP (2.x). – mark Dec 18 '12 at 18:12

2 Answers2

0

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

jeni
  • 442
  • 1
  • 4
  • 12
-1

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"
deizel.
  • 11,042
  • 1
  • 39
  • 50