Distance between 2 geocodes

前端 未结 9 1417
一生所求
一生所求 2020-12-05 22:10

What is the formula for calculating the distance between 2 geocodes? I have seen some of the answers on this site but they basically say to rely on SQL Server 08 functions,

相关标签:
9条回答
  • 2020-12-05 22:35

    You're looking for the length of the Great Circle Path between two points on a sphere. Try looking up "Great Circle Path" or "Great Circle Distance" on Google.

    0 讨论(0)
  • 2020-12-05 22:37

    the pythagorean theorem?

    0 讨论(0)
  • 2020-12-05 22:39

    Use an approximation of the earth and the Haversine formula. You can get a javascript version on the following url, which you can translate to your language of choice:

    http://www.movable-type.co.uk/scripts/latlong.html

    Here is another way: http://escience.anu.edu.au/project/04S2/SE/3DVOT/3DVOT/pHatTrack_Application/Source_code/pHatTrack/Converter.java

    0 讨论(0)
  • 2020-12-05 22:43

    Here is a way to do it if you are using sql server.

    CREATE function dist (@Lat1 varchar(50), @Lng1 varchar(50),@Lat2 varchar(50), @Lng2 varchar(50)) 
    returns float 
    as
    begin
    
    declare @p1 geography
    declare @p2 geography
    
    set @p1 = geography::STGeomFromText('POINT('+ @Lng1+' '+ @Lat1 +')', 4326)
    set @p2 = geography::STGeomFromText('POINT('+ @Lng2+' '+ @Lat2 +')', 4326)
    
    return @p1.STDistance(@p2)
    end
    
    0 讨论(0)
  • 2020-12-05 22:47

    This will do it for you in c#.

    Within the namespace put these:

     public enum DistanceType { Miles, Kilometers };
    
    public struct Position
    {
        public double Latitude;
        public double Longitude;
    }
    
    class Haversine
    {
    
        public double Distance(Position pos1, Position pos2, DistanceType type)
        {
            double preDlat = pos2.Latitude - pos1.Latitude;
            double preDlon = pos2.Longitude - pos1.Longitude;
            double R = (type == DistanceType.Miles) ? 3960 : 6371;
            double dLat = this.toRadian(preDlat);
            double dLon = this.toRadian(preDlon);
            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
            Math.Cos(this.toRadian(pos1.Latitude)) * Math.Cos(this.toRadian(pos2.Latitude)) *
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
            double c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
            double d = R * c;
            return d;
        }
    
        private double toRadian(double val)
        {
            return (Math.PI / 180) * val;
        }
    

    Then to utilise these in the main code:

    Position pos1 = new Position();
                        pos1.Latitude = Convert.ToDouble(hotelx.latitude);
                        pos1.Longitude = Convert.ToDouble(hotelx.longitude);
                        Position pos2 = new Position();
                        pos2.Latitude = Convert.ToDouble(lat);
                        pos2.Longitude = Convert.ToDouble(lng);
                        Haversine calc = new Haversine();
    
                        double result = calc.Distance(pos1, pos2, DistanceType.Miles);
    
    0 讨论(0)
  • 2020-12-05 22:52

    Sorry, I don't know what country you are in even. Are we talking about Easting and Northings (UK, Ordance Survey system) or Lat/Long or some other system? If we are talking Easting and Northing then you can use
    sqr((x1-x2)^2 + (y1-y2)^2)
    This does not allow for the fact that the earth is a sphere, but for short distances you won't notice. We use it at work for distances between points within the county.
    Be carful about how longer grid reference you use. I think an 8 figure reference will give you a distance in metres. I'll be able to get a definate answer at work next week if no one else has supplied it.

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