Distance beetween coordinates

I made a script that outputs distance beetween points, using navigator.geolocation.getCurrentPosition and haversine formula. Unlucky, the distance function doesn't work - it returns itself. Are there any mistake I don't see?

<script>
    var options = {
     enableHighAccuracy: true,
     timeout: 5000,
     maximumAge: 0
    };
    navigator.geolocation.getCurrentPosition(success,error,options);
    
    function success(pos, distance){
        var crd = pos.coords;
        var lat1=crd.latitude;
        var lon1=crd.longitude;
        var lat2=50; //Sample target latitude
        var lon2=20; //Sample target longitude
    }
    
    function error(err){
        document.getElementById("demo").innerHTML = "Błąd.";
    }
    
    //haversine formula
    function distance(success, lat1, lon1, lat2, lon2) {
  var p = 0.017453292519943295;    // Math.PI / 180
  var a = 0.5 - Math.cos((lat2 - lat1) * p)/2 + Math.cos(lat1 * p) * Math.cos(lat2 * p) * (1 - Math.cos((lon2 - lon1) * p))/2;
  return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km
}
    function final(distance){
         document.getElementById("demo").innerHTML = "Odległość: " + distance;
    }
  final(distance);

</script>


Read more here: https://stackoverflow.com/questions/65719728/distance-beetween-coordinates

Content Attribution

This content was originally published by aciddioxide at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: