找到最近的纬度和经度

本文关键字:经度 纬度 最近 | 更新日期: 2023-09-27 17:49:33

我的问题是,我的数据库中只有酒店信息及其纬度和经度。现在,我要从给定的经纬度找到最近的酒店。例如:假设纬度为196.98575,经度为24.985644,现在基于这段时间,我想找到最近的酒店在15公里,我有在我的数据库中可用。请建议我任何想法,或者如果你有任何存储过程请告诉我,以便我可以避免手动任务。我正在使用SQL server.

找到最近的纬度和经度

正如上面的评论所暗示的那样,SQL Server自SQL 2008以来已经具有了本地地理空间功能。下面是我尝试的解决方案:

create table dbo.Hotels (
    HotelID int identity not null,
        constraint [PK_Hotels] primary key clustered (HotelID),
    Longitude decimal(15, 12) not null,
    Latitude decimal(14, 12) not null,
    geo as geography::Point(Latitude, Longitude, 4326)
)
insert into dbo.Hotels 
    (Longitude, Latitude)
values
    (-122.4167, 37.7833);
go
create procedure dbo.findHotels (@point geography, @distanceKM int = 15)
as
begin
    --SRID 4326 measures disances in meters, so use that measure
    declare @distanceM int = @distanceKM * 1000;
    select HotelID, @point.STDistance(geo) as [DistanceInM]
    from dbo.Hotels
    where @point.STDistance(geo) <= @distanceM
    order by @point.STDistance(geo);
end
go
declare @longitude decimal(15, 12) = -122.4168,
    @latitude decimal(14, 12) = 37.7832;
declare @p geography = geography::Point(@latitude, @longitude, 4326);
exec dbo.findHotels @p, 15;

看这里。它在数据库中找到距离M=(lat, lon)=(1.3963, -0.6981) d=1000公里以内的地方*以弧度表示坐标的经纬度

下面是根据这个源

进行的简单查询
SELECT * FROM Places WHERE
    (Lat => 1.2393 AND Lat <= 1.5532) AND (Lon >= -1.8184 AND Lon <= 0.4221)
AND
    acos(sin(1.3963) * sin(Lat) + cos(1.3963) * cos(Lat) * cos(Lon - (-0.6981))) <= 0.1570;