我有以下 SQL (SQL Server),它在大多数情况下都有效。问题是我真的在创建一个正方形而不是一个真正的圆形。我的目标是经过一个有纬度和经度的城市和州,然后找到该纬度半径 100 英里范围内的所有城市。纬度和经度存储在数据库中,所以我所有的值都在那里。我只需要一种更精确的方法。到目前为止,这是我的代码:
ALTER PROCEDURE [dbo].[sp_StoresByZipArea] (@zip nvarchar(5), @Radius float) AS
DECLARE @LatRange float
DECLARE @LongRange float
DECLARE @LowLatitude float
DECLARE @HighLatitude float
DECLARE @LowLongitude float
DECLARE @HighLongitude float
DECLARE @istartlat float
DECLARE @istartlong float
SELECT @iStartlat=Latitude, @iStartLong=Longitude from zipcodes where zipcode=@ZIP
SELECT @LatRange = @Radius / ((6076 / 5280) * 60)
SELECT @LongRange = @Radius / (((cos((@iStartLat * 3.141592653589 / 180)) * 6076.) / 5280.) * 60)
SELECT @LowLatitude = @istartlat - @LatRange
SELECT @HighLatitude = @istartlat + @LatRange
SELECT @LowLongitude = @istartlong - @LongRange
SELECT @HighLongitude = @istartlong + @LongRange
/** Now you can create a SQL statement which limits the recordset of cities in this manner: **/
SELECT * FROM ZipCodes
WHERE (Latitude <= @HighLatitude) AND (Latitude >= @LowLatitude) AND (Longitude >= @LowLongitude) AND (Longitude <= @HighLongitude)