-2

I want to take the calculations between two points from Google distance api. There is any implementation source code link available to implement.

Documentation link is: https://developers.google.com/maps/documentation/directions/intro

Developer
  • 67
  • 10

2 Answers2

0

Here's my solution. Hope this help :)

function getDistance(from, to){
    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
    {
        origins: [from],
        destinations: [to],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.METRIC,
        avoidHighways: false,
        avoidTolls: false
    }, function(response, status){callbackFunc();});
}

function callbackFunc(response, status){
    if (status != google.maps.DistanceMatrixStatus.OK) 
    {
        console.log('Error was: ' + status);
    } 
    else
    {
        var from = response.originAddresses;
        var to = response.destinationAddresses;
        for (var i = 0; i < from.length; i++) {
            var results = response.rows[i].elements;
            for (var j = 0; j < results.length; j++) {
                console.log(results[j].distance.value);

            }
        }
    }
}
Neo
  • 1,469
  • 3
  • 23
  • 40
0

You can also use the great circle distance which is :

the shortest distance between two points on the surface of a sphere

it allows you to calculate the distance between to lat/long point return by the API.

Here is a link to the wikipedia page with the formula.

You will also find some implementation from this thread or this one

Community
  • 1
  • 1
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77