Calculate Distance Between Two Points in Google Maps V3

Calculate distance between two points in google maps V3

If you want to calculate it yourself, then you can use the Haversine formula:

var rad = function(x) {
return x * Math.PI / 180;
};

var getDistance = function(p1, p2) {
var R = 6378137; // Earth’s mean radius in meter
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; // returns the distance in meter
};

Google Maps V3 distance between two points - different values

The solution is to compute locally using distancematrix api: https://developers.google.com/maps/documentation/javascript/distancematrix (Have to enable Distance Matrix from API Dashboard)

var from = new google.maps.LatLng(46.5610058, 26.9098054);
var fromName = 'Bacau';
var dest = new google.maps.LatLng(44.391403, 26.1157184);
var destName = 'Bucuresti';

var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [from, fromName],
destinations: [destName, dest],
travelMode: 'DRIVING'
}, callback);

function callback(response, status) {
if (status == 'OK') {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;

for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
console.log(results);
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.text;
var duration = element.duration.text;
var from = origins[i];
var to = destinations[j];
}
}
}
}

working out distances between two points using google maps api?

there is a handy function

http://code.google.com/apis/maps/documentation/reference.html#GLatLng.distanceFrom

basically create 2 GlatLng objects then use the above method

Google Maps V3: Comparing the distance of multiple points

Use google's distance matrix api. https://developers.google.com/maps/documentation/distancematrix/

How can get the distances between 2 points via Google Maps Javascript API3?

Sounds simple, I recommend the geometry library,

https://developers.google.com/maps/documentation/javascript/reference#spherical

computeDistanceBetween(from:LatLng, to:LatLng, radius?:number)

From my experience the result is in meters. Remember to add the optional geometry library in your tag

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

Calculate distance between 2 points in google maps using python

from math import *

def greatCircleDistance((lat1, lon1), (lat2, lon2)):
def haversin(x):
return sin(x/2)**2
return 2 * asin(sqrt(
haversin(lat2-lat1) +
cos(lat1) * cos(lat2) * haversin(lon2-lon1)))

This returns the angular distance between the points on a sphere. To get a distance in length (kilometers), multiply the result with the Earth radius.

But you could have it looked up in a formula collection as well, hence the bunch of downvotes ;-)

Calculating distance between two points in Google Maps for Ionic 2

Have you seen this:

https://stackoverflow.com/a/27943/52160

 function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}

function deg2rad(deg) {
return deg * (Math.PI/180)
}
}


Related Topics



Leave a reply



Submit