radius search by latitude / longitude

后端 未结 3 583
不思量自难忘°
不思量自难忘° 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: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 = , @Longitude float = ;
    declare @point geography = geography::Point(@Latitude, @Longitude, 4326);
    declare @distance int = ;
    
    select * from [yourTable] where @point.STDistance([p]) <= @distance;
    

提交回复
热议问题