How to find distance from the latitude and longitude of two locations?

后端 未结 12 2420
情歌与酒
情歌与酒 2020-11-29 02:40

I have a set of latitudes and longitudes of locations.

  • How to find distance from one location in the set to another?
  • Is there a f
12条回答
  •  生来不讨喜
    2020-11-29 03:16

    here is a fiddle with finding locations / near locations to long/lat by given IP:

    http://jsfiddle.net/bassta/zrgd9qc3/2/

    And here is the function I use to calculate the distance in straight line:

    function distance(lat1, lng1, lat2, lng2) {
            var radlat1 = Math.PI * lat1 / 180;
            var radlat2 = Math.PI * lat2 / 180;
            var radlon1 = Math.PI * lng1 / 180;
            var radlon2 = Math.PI * lng2 / 180;
            var theta = lng1 - lng2;
            var radtheta = Math.PI * theta / 180;
            var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
            dist = Math.acos(dist);
            dist = dist * 180 / Math.PI;
            dist = dist * 60 * 1.1515;
    
            //Get in in kilometers
            dist = dist * 1.609344;
    
            return dist;
        }
    

    It returns the distance in Kilometers

提交回复
热议问题