radius search by latitude / longitude

后端 未结 3 576
不思量自难忘°
不思量自难忘° 2020-12-05 10:45

I have found a bunch of answers for this question using mysql , but I wasn\'t able to convert anything into a query ms sql 2008 can use. I have a longitude and latitude col

相关标签:
3条回答
  • 2020-12-05 11:11
    DECLARE @CurrentLocation geography; 
    SET @CurrentLocation  = geography::Point(12.822222, 80.222222, 4326)
    
    SELECT * , Round (GeoLocation.STDistance(@CurrentLocation ),0) AS Distance FROM [Landmark]
    WHERE GeoLocation.STDistance(@CurrentLocation )<= 2000 -- 2 Km
    

    Wonderful Tutorial

    http://www.sql-server-helper.com/sql-server-2008/convert-latitude-longitude-to-geography-point.aspx

    0 讨论(0)
  • 2020-12-05 11:14

    Since you're on SQL 2008, consider using the native geospatial capabilities. You can do fancy things like:

    • Create a persisted computed column of geography type that represents your point.
    • Create a spatial index on the computed column. This will make things like yourPoint.STDistance(@otherPoint) <= @distance efficient

    Like so:

    alter table [yourTable] add [p] as geography::Point(Latitude, Longitude, 4326) persisted;
    create spatial index [yourSpatialIndex] on [yourTable] ([p])
    
    declare @Latitude float = <somevalue>, @Longitude float = <somevalue>;
    declare @point geography = geography::Point(@Latitude, @Longitude, 4326);
    declare @distance int = <distance in meters>;
    
    select * from [yourTable] where @point.STDistance([p]) <= @distance;
    
    0 讨论(0)
  • 2020-12-05 11:36

    I think you want POWER not POW

    http://msdn.microsoft.com/en-us/library/ms174276.aspx

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