0

I am trying to calculate the displacement between two coordinates in meters, Is there a function in here maps that does this calculation?

If not, how do I effectively calculate distance between the two coordinate points in JavaScript.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92
  • My guess is this was -1 simply because it was a short question. There's an extended discussion on this type of thing going on in [meta currently](http://meta.stackexchange.com/questions/210840/should-stack-overflow-be-awarding-as-for-effort) – Liam Dec 11 '13 at 11:09
  • Makes sense. Anyway I found what I was looking for, http://stackoverflow.com/a/10054282/1161412 .(Now I just have to implement the same in JS) Although, I would still like to know if there are any functions in `HERE` maps for the same. So, I am going to keep this question open for a week. – Nagendra Rao Dec 11 '13 at 11:24

2 Answers2

2

I think you are looking for the distance() method in the nokia.maps.geo.Coordinate class. An example of usage can be found in the Find Nearest Marker example on the HERE Maps Community pages on GitHub.

Jason Fox
  • 5,115
  • 1
  • 15
  • 34
2

I found this php function (based on Vincenty's algorithm), and it works great in calculating displacement between 2 coordinates,

<?php
function vincentyGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000){
        // convert from degrees to radians
        $latFrom = deg2rad($latitudeFrom);
        $lonFrom = deg2rad($longitudeFrom);
        $latTo = deg2rad($latitudeTo);
        $lonTo = deg2rad($longitudeTo);

        $lonDelta = $lonTo - $lonFrom;
        $a = pow(cos($latTo) * sin($lonDelta), 2) +
        pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
        $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

        $angle = atan2(sqrt($a), $b);
        return $angle * $earthRadius;
    }
?>

Call:

<?php $distance_in_meters = vincentyGreatCircleDistance(53.6235,-1.34307, 51.6554, -2.3445 ); ?>
Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92