I have the following SQL (SQL Server) and it works for the most part. The problem is that I'm really creating a square, not a real circle. My goal is to go through a city and state that has lat and long, and then find all the cities within a radius of 100 miles from this lat. Latitude and longitude are stored in the database, so all my values ββare. I just need a more accurate way to do this. Here is my code:
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 SELECT * FROM ZipCodes WHERE (Latitude <= @HighLatitude) AND (Latitude >= @LowLatitude) AND (Longitude >= @LowLongitude) AND (Longitude <= @HighLongitude)
source share