The distance between two geocodes

What is the formula for calculating the distance between two geocodes? I saw some answers on this site, but they basically say that they rely on SQL Server 08 features, but I'm not yet on 08. Any help would be appreciated.

+5
source share
9 answers

Use the earth approximation and the Haversine formula . You can get the javascript version at the following URL, which you can translate into 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

+8
source

SQL Server 2000 SQL Server Zipcode /

+2

#.

:

 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;
    }

:

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);
+2

  • , 2 " "
  • " " .

, :

  • EarthRadius * latitude difference
  • EarthRadius * longitude difference * cos(latitude). cos(lat), , . P1 P2 , cos (latP1) cos (latP2)
  • Pythagore

JavaScript:

function ClosePointsDistance(latP1, lngP1, latP2, lngP2) {
    var d2r = Math.PI / 180,
    R=6371; // Earth Radius in km
    latP1 *= d2r; lngP1 *= d2r; latP2 *= d2r; lngP2 *= d2r; // convert to radians
    dlat = latP2 - latP1,
    dlng = (lngP2 - lngP1) * Math.cos(latP1);
    return R * Math.sqrt( dlat*dlat + dlng*dlng );
}

(): 110,9 , () 111,0 .

!!! 0 ( ): P1 Lng 359, P2 Lng 0, .

+2

, SQL-.

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
+1

. " " " " Google.

0

, , . Easting Northings (UK, Ordance Survey system) Lat/Long - ? Easting Northing, sqr ((x1-x2) ^ 2 + (y1-y2) ^ 2)
, , . .
, . , 8- . , .

0

, , .

, - ( , ). Haskell, , , :

distRadians (lat1,lon1) (lat2,lon2) = 
    radius_of_earth * 
    acos (cos lat1 * cos lon1 * cos lat2 * cos lon2 + 
          cos lat1 * sin lon1 * cos lat2 * sin lon2 + 
          sin lat1 * sin lat2) where
    radius_of_earth = 6378 -- kilometers


distDegrees a b = distRadians (coord2rad a) (coord2rad b) where
    deg2rad d = d * pi / 180
    coord2rad (lat,lon) = (deg2rad lat, deg2rad lon)

distRadians , .

distDegrees - , .

. .

, , , . FAQ: http://www.movable-type.co.uk/scripts/gis-faq-5.1.html

0

?

-1

All Articles