将浮动纬度和经度转换为地理位置

时间:2011-08-01 17:22:13

标签: sql-server-2008

如何将浮动纬度和经度值转换为地理类型值?我有@lat@lon个变量。

2 个答案:

答案 0 :(得分:18)

使用案例陈述:

CASE
    WHEN ((@Latitude IS NOT NULL) AND (@Longitude IS NOT NULL))
    THEN geography::Point(@Latitude, @Longitude, 4326)
    ELSE NULL           
END

或if:

的变体
 DECLARE @Location geography = NULL
 IF (@Latitude IS NOT NULL AND @Longitude IS NOT NULL)
    SET @Location = geography::Point(@Latitude, @Longitude, 4326);

答案 1 :(得分:2)

我自己找到了解决方案:

geography::STPointFromText(
    'POINT(' + CAST(@lon AS VARCHAR(20)) + ' ' + CAST(@lat AS VARCHAR(20)) + ')', 4326)