I cannot get the same accuracy as Google maps when it comes to distance?

前端 未结 1 937
轮回少年
轮回少年 2020-12-17 05:44

I am developing an app which calculate the distance between 2 points. I cannot use the Google Maps API.

I have found the coordinates for each of the markers in the m

相关标签:
1条回答
  • 2020-12-17 06:03

    If I have correctly entered your start and end points, this implementation of the haversine formula (which I have tested in the real world) produces a distance of 895m (straight line).

    var lt = 51.472447;
    var lt1 = 51.465097;
    var ln = -3.176151;
    var ln1 = -3.170893;
    var dLat = (lt - lt1) * Math.PI / 180;
    var dLon = (ln - ln1) * Math.PI / 180;
    var a = 0.5 - Math.cos(dLat) / 2 + Math.cos(lt1 * Math.PI / 180) * Math.cos(lt * Math.PI / 180) * (1 - Math.cos(dLon)) / 2;
    d = Math.round(6371000 * 2 * Math.asin(Math.sqrt(a)));
    
    $('#distance').html(d);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div id="distance"></div>

    0 讨论(0)
提交回复
热议问题