简体   繁体   English

从地理位置机芯字符串中提取纬度/经度

[英]extract Lat/Lng from Geo loaction string

I have a Geo location string as 25°9'37"N 55°13'28"E . 我的地理位置字符串为E°25°9'37“ N 55°13'28” E。 Using JavaScript Regex I want to extract the Lat and Lng from the above string. 我想使用JavaScript Regex从上面的字符串中提取Lat和Lng。 Thanks. 谢谢。

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. 您的第一行可能是“ 25°9'37” N是纬度的分钟格式,您可能想提取其值并将其转换为十进制度表示形式,这样就可以对其进行进一步的计算。 @Stormwind @Stormwind

According to this blogpost, conversion formula is 根据博客文章,转换公式为

//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

Regex (simple) 正则表达式(简单)

//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`

Solution for latitude (simple) - notice, this solution is error prone, as it was written within 5mins, so be careful 纬度解决方案(简单)-请注意,此解决方案容易出错,因为它是在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

Solution for longitude 经度解决方案

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

For more details refer to Converting Latitude/Longitude from Degrees/Minutes/Seconds to Decimal Degrees 有关更多详细信息,请参阅将纬度/经度从度/分/秒转换为小数度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM