42

I have these coordinate :

(45.463688, 9.18814) 
(46.0438317, 9.75936230000002)

and I need (trought Google API V3, I think) to get the distance between those 2 points in metre. How can I do it?

markzzz
  • 47,390
  • 120
  • 299
  • 507

8 Answers8

125

If you're looking to use the v3 google maps API, here is a function to use: Note: you must add &libraries=geometry to your script source

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

<script>
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);

alert(calcDistance(p1, p2));

//calculates distance between two points in km's
function calcDistance(p1, p2) {
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}

</script>
pradeepcep
  • 902
  • 2
  • 8
  • 24
Roman Goyenko
  • 6,965
  • 5
  • 48
  • 81
4

I think you could do without any specific API, and calculate distance with plain Javascript:

This site has good info about geographical calculations and Javascript sample for distance calculation.

Ok, quick glance at Google API page and it seems, you could do it by:

  1. Call DirectionsService().route() to get DirectionsResult with routes
  2. For one or each route go through its legs property and calculate sum of distances
PiRX
  • 3,515
  • 1
  • 19
  • 18
  • Thank you for the first link. Calculation of close distances can be optimized much using aproximate formulas – Dan Jun 05 '13 at 09:33
1

http://www.csgnetwork.com/gpsdistcalc.html

Has nothing to do with coding btw

EDIT if you're looking for some code

Calculate distance between two points in google maps V3

Community
  • 1
  • 1
Rob
  • 4,927
  • 12
  • 49
  • 54
1

This is primarily done with math outside of the google api, so you shouldn't need to use the API for anything other than finding the correct coordinates.

Knowing that, here's a link to a previously answered question relating to Javascript.

Calculate distance between 2 GPS coordinates

Community
  • 1
  • 1
Pixelmixer
  • 319
  • 1
  • 11
1

Try this

 CLLocationCoordinate2D coord1;
 coord1.latitude = 45.463688;
 coord1.longitude = 9.18814;
 CLLocation *loc1  = [[[CLLocation alloc] initWithLatitude:coord1.latitude longitude:coord1.longitude] autorelease];

 CLLocationCoordinate2D coord2;
 coord2.latitude = 46.0438317;
 coord2.longitude = 9.75936230000002;
 CLLocation *loc2  = [[[CLLocation alloc] initWithLatitude:46.0438317 longitude:9.75936230000002] autorelease];

 CLLocationDistance d1 = [loc1 distanceFromLocation:loc2];
Tommaso
  • 155
  • 2
  • 10
0

Try this:

const const toRadians = (val) => {
    return val * Math.PI / 180;
}
const toDegrees = (val) => {
    return val * 180 / Math.PI;
}
// Calculate a point winthin a circle
// circle ={center:LatLong, radius: number} // in metres
const pointInsideCircle = (point, circle) => {
    let center = circle.center;
    let distance = distanceBetween(point, center);

    //alert(distance);
    return distance < circle.radius;
};

const distanceBetween = (point1, point2) => {
   //debugger;
   var R = 6371e3; // metres
   var φ1 = toRadians(point1.latitude);
   var φ2 = toRadians(point2.latitude);
   var Δφ = toRadians(point2.latitude - point1.latitude);
   var Δλ = toRadians(point2.longitude - point1.longitude);

   var a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
           Math.cos(φ1) * Math.cos(φ2) *
           Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

   return R * c;
}

References: http://www.movable-type.co.uk/scripts/latlong.html

  • 1
    Please improve your answer. Why it is better, how it works, etc. "Try this" is not enough. Stackoverflow is not about trying things blindly but reading and understanding how things work. Still thank you for contribution :) – Tõnu Samuel Jan 04 '18 at 03:08
0

Are you referring to distance as in length of the entire path or you want to know only the displacement (straight line distance)? I see no one is pointing the difference between distance and displacement here. For distance calculate each route point given by JSON/XML data, as for displacement there is a built-in solution using Spherical class.

0

Are you referring to distance as in length of the entire path or you want to know only the displacement (straight line distance)? I see no one is pointing the difference between distance and displacement here. For distance calculate each route point given by JSON/XML data, as for displacement there is a built-in solution using Spherical class. The idea is if your are referring to distance then you just use the computeDistanceBetween on each path point and concatenate it.

//calculates distance between two points in km's
function calcDistance(p1, p2) {
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}