将纬度和经度转换为十进制值
我的 GPS 信息以以下形式呈现:
36°57'9" N 110°4'21" W
我可以使用 Chris Veness 的 JavaScript 函数 将度、分和秒转换为数字度数,但首先需要将 GPS 信息解析为单独的纬度和经度字符串(带有 NSEW 后缀)。 我已阅读 stackoverflow 上的相关帖子,但我不是正则表达式专家(也不是程序员),需要一些解析功能的帮助。 将此字符串解析为纬度和经度以便在转换函数中使用的最佳方法是什么?
所有这一切的结果将是一个 Web 链接,单击该链接即可查看位置的 Google 地图表示。
I have GPS information presented in the form:
36°57'9" N 110°4'21" W
I can use the javascript functions of Chris Veness to convert degrees, minutes and seconds to numeric degrees, but first need to parse the GPS info into the individual latitude and longitude strings (with NSEW suffixes). I have read related posts on stackoverflow, but am not a regex expert (nor a programmer) and need some help with the parsing function. What's the best way to parse this string into latitude and longitude for use in the conversion function?
The result of all this will be a Web link that one can click on to see a Google map representation of location.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
抱歉还有另一个算法,但我需要在用户提供的输入上运行它,所以我需要检查它是否完全匹配所需的格式。 为了实现这一点,我在整个字符串上使用正则表达式。
我的代码支持以下格式(及其组合以及带有附加空格的变体):
-1.234,-2.345
geo:-1.234,-2.345
geo:- 1.234,-2.345?z=10
-1.234;-2.345
(分号分隔符)-1.234 -2.345
(空格分隔符)-1,234 -2,345
(十进制逗号)−1.234,−2.345
(Unicode 减号)-1.234°, -2.345°
1° 23', 2° 34'
1° 23' 45.67", 2° 34' 56.78"
1 度 23
1° 45.67 ", 2° 56.78"
(秒但没有分钟)1° 23′ 45.67″, 2° 34′ 56.78″
(Unicode 分钟和秒)1° 23' 45.67 ”, 2° 34' 56.78”
(Unicode 引号表示分钟和秒)-1° 23' 45.67", -2° 34' 56.78"
1° 23' 45.67 " S, 2° 34' 56.78" W
(半球后缀)S 1° 23' 45.67", W 2° 34' 56.78"
(半球前缀)2° 34 ' 56.78" W 1° 23' 45.67" S
(纬度/经度已切换)如果输入的格式无效,则返回 undefined。
Sorry for yet another algorithm, but I needed to run this on user-provided input, so I need to check whether it matches the desired format at all. To achieve this, I’m using a regexp on the whole string.
My code supports the following formats (and combinations thereof and variants with additional white spaces):
-1.234,-2.345
geo:-1.234,-2.345
geo:-1.234,-2.345?z=10
-1.234;-2.345
(semicolon separator)-1.234 -2.345
(space separator)-1,234 -2,345
(decimal comma)−1.234,−2.345
(Unicode minus)-1.234°, -2.345°
1° 23', 2° 34'
1° 23' 45.67", 2° 34' 56.78"
1 deg 23' 45.67", 2 deg 34' 56.78"
1° 45.67", 2° 56.78"
(seconds but no minutes)1° 23′ 45.67″, 2° 34′ 56.78″
(Unicode minutes and seconds)1° 23’ 45.67”, 2° 34’ 56.78”
(Unicode quotes for minutes and seconds)-1° 23' 45.67", -2° 34' 56.78"
1° 23' 45.67" S, 2° 34' 56.78" W
(hemisphere suffix)S 1° 23' 45.67", W 2° 34' 56.78"
(hemisphere prefix)2° 34' 56.78" W 1° 23' 45.67" S
(latitude/longitude switched)If the input has an invalid format, undefined is returned.
使用
parse-dms
库:Using the
parse-dms
library:要解析您的输入,请使用以下命令。
以下内容会将您的 DMS 转换为 DD
因此您的输入将产生以下结果:
十进制坐标可以输入谷歌地图以通过
GLatLng(lat, lng)
(Google 地图 API)To parse your input use the following.
The following will convert your DMS to DD
So your input would produce the following:
Decimal coordinates can be fed into google maps to get points via
GLatLng(lat, lng)
(Google Maps API)更正了上述函数并使输出成为对象。
Corrected the above functions and made the output an object.
我的调整版本将字符串部分强制转换为数字,以便它们实际上可以添加在一起而不是连接起来。 它还处理秒组件常见的十进制值:
以下将把您的 DMS 转换为 DD
My tweaked version coerces the string parts into Numbers so that they can actually be added together rather than concatenated. It also handles decimal values which are common for the Seconds component:
The following will convert your DMS to DD
这是我对此的看法:
}
该函数不处理所有类型的纬度/经度类型,但它处理以下格式:
这就是我需要的。
here is my take on this:
}
This function doesn't handle all types of lat / long types, but it handles the following formats:
Which is what i needed.
我使用 \d+(\,\d+) 和 \d+(.\d+) 因为可以有浮点数
我的最终函数:
I used \d+(\,\d+) and \d+(.\d+) because can have float numbers
My final function:
我在这个函数上得到了一些 NaN 并且需要这样做(不要问我为什么)
I got some NaN's on this function and needed to do this (don't ask me why)
乔,你提到的脚本已经达到了你想要的效果。 有了它,您可以转换纬度和经度,并将其放入链接中以在 Google 地图中查看位置:
Joe, the script you've mentioned already did what do you want. With it you can convert lat and long and put it into link to see location in Google map:
使用 Shannon Antonio Black 的正则表达式模式(上面),我的解决方案是:
我认为这是 EC6 的更简化版本
Using Shannon Antonio Black's regex pattern (above), my solution is:
I think that is more simplified version with EC6
结果:
解释:
" "
进行分割,得到一个由四个元素组成的字符串数组,grade
、分钟
和seconds
,所有数值的等级+(分+秒/60)/60
Result:
Explanation:
" "
, getting an array of strings of four elementsgrade
,minutes
andseconds
respectively, all numerical valuesgrade + (minutes + seconds / 60) / 60