将纬度和经度转换为十进制值 [英] Converting latitude and longitude to decimal values

查看:137
本文介绍了将纬度和经度转换为十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下形式提供的GPS信息:

36°57'9N 110°4'21W

我可以使用Chris Veness的 javascript功能将度,分和秒转换为数字度,但首先需要将GPS信息解析为各个纬度和经度字符串(带有NSEW后缀)。我已经阅读了有关stackoverflow的帖子,但我不是正则表达式专家(也不是程序员),并且需要一些解析函数的帮助。将此字符串解析为经度和纬度以用于转换函数的最佳方法是什么?



这些结果将是一个可以点击的Web链接请参阅Google地图位置表示。

解决方案

要解析您的输入,请使用以下内容。 b $ b

  function ParseDMS(input){
var parts = input.split(/ [^ \d\w] + /);
var lat = ConvertDMSToDD(parts [0],parts [1],parts [2],parts [3]);
var lng = ConvertDMSToDD(零件[4],零件[5],零件[6],零件[7]);

$ / code>

以下内容会将您的DMS转换为DD

 函数ConvertDMSToDD(度,分,秒,方向){
var dd =度+分钟/ 60 +秒/(60 * 60);

if(direction ==S|| direction ==W){
dd = dd * -1;
} //不要为N或E
返回dd;

$ / code>

所以你的输入会产生以下结果:

  36°57'9N = 36.9525000 
110°4'21W = -110.0725000

十进制坐标可以通过 GLatLng(lat,lng) Google Maps API


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.

解决方案

To parse your input use the following.

function ParseDMS(input) {
    var parts = input.split(/[^\d\w]+/);
    var lat = ConvertDMSToDD(parts[0], parts[1], parts[2], parts[3]);
    var lng = ConvertDMSToDD(parts[4], parts[5], parts[6], parts[7]);
}

The following will convert your DMS to DD

function ConvertDMSToDD(degrees, minutes, seconds, direction) {
    var dd = degrees + minutes/60 + seconds/(60*60);

    if (direction == "S" || direction == "W") {
        dd = dd * -1;
    } // Don't do anything for N or E
    return dd;
}

So your input would produce the following:

36°57'9" N  = 36.9525000
110°4'21" W = -110.0725000

Decimal coordinates can be fed into google maps to get points via GLatLng(lat, lng) (Google Maps API)

这篇关于将纬度和经度转换为十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆