gpt4 book ai didi

javascript - 从地理位置字符串中提取纬度/经度

转载 作者:行者123 更新时间:2023-12-02 14:43:24 26 4
gpt4 key购买 nike

我的地理位置字符串为 25°9'37"N 55°13'28"E。使用 JavaScript Regex 我想从上面的字符串中提取 Lat 和 Lng。谢谢。

最佳答案

Your first row could be "25°9'37"N is the minute-format of a latitude, you probably want to extract it's values and convert to it's decimal degree representation, this enables further calculation on it. @Stormwind

根据this博文,转换公式为

//latitude formula
([LATITUDE_DEG])+([LATITUDE_MIN]/60)+([LATITUDE_SEC]/3600))*
IF [Latitude_Direction]="South" THEN -1 ELSE 1 END

//longitude
([LONGITUDE_DEG]+([LONGITUDE_MIN]/60)+([LONGITUDE_SEC]/3600))*
IF [Longitude_Direction]="West" THEN -1 ELSE 1 END

正则表达式(简单)

//latitude
25°9'37"N
/(\w+)°(\w+)'(\w+)"(\w+)/
`25`, `9`, `37`, `N`

//regex from @PranavCBalan also works well
/(\d+)°(\d+)'(\d)+"([NEWS])/
`25`, `9`, `37`, `N`

纬度的解决方案(简单) - 注意,这个解决方案很容易出错,因为它是在 5 分钟内编写的,所以要小心

var lat_str = "25°9'37\"N"; //escaped string
lat_str = lat_str.replace('\"', '"'); //little trick
var matches = lat_str.match(/(\w+)°(\w+)'(\w+)"(\w+)/); //regexify
var lat_deg = parseInt(matches[1]); //degrees
var lat_min = parseInt(matches[2]); //minutes
var lat_sec = parseInt(matches[3]); //seconds
var lat_dir = matches[4]; //direction

var latitude = (lat_deg+(lat_min/60)+(lat_sec/3600))*(lat_dir === 'N' ? 1 : -1);

//25.160277777777775

经度的解

//same as latitude (except direction check), do it as your homework

更多详情请参阅Converting Latitude/Longitude from Degrees/Minutes/Seconds to Decimal Degrees

关于javascript - 从地理位置字符串中提取纬度/经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36819894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com