40

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.

4

7 回答 7

70

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

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]);
}

以下将您的 DMS 转换为 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;
}

因此,您的输入将产生以下结果:

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

十进制坐标可以输入谷歌地图以通过GLatLng(lat, lng)谷歌地图API)获取点

于 2009-07-16T21:12:09.143 回答
19

更正了上述功能并使输出成为对象。

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

    return {
        Latitude : lat,
        Longitude: lng,
        Position : lat + ',' + lng
    }
}


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

    if (direction == "S" || direction == "W") {
        dd = dd * -1;
    } // Don't do anything for N or E
    return dd;
}
于 2015-10-22T12:00:52.983 回答
8

我的调整版本将字符串部分强制转换为数字,以便它们实际上可以加在一起而不是连接起来。它还处理 Seconds 组件常见的十进制值:

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]);
}

以下将您的 DMS 转换为 DD

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

    if (direction == "S" || direction == "W") {
        dd = dd * -1;
    } // Don't do anything for N or E
    return dd;
}
于 2015-09-09T15:52:08.070 回答
6

这是我对此的看法:

function parse_gps( input ) {

if( input.indexOf( 'N' ) == -1 && input.indexOf( 'S' ) == -1 &&
    input.indexOf( 'W' ) == -1 && input.indexOf( 'E' ) == -1 ) {
    return input.split(',');
}

var parts = input.split(/[°'"]+/).join(' ').split(/[^\w\S]+/);

var directions = [];
var coords = [];
var dd = 0;
var pow = 0;

for( i in parts ) {

    // we end on a direction
    if( isNaN( parts[i] ) ) {

        var _float = parseFloat( parts[i] );

        var direction = parts[i];

        if( !isNaN(_float ) ) {
            dd += ( _float / Math.pow( 60, pow++ ) );
            direction = parts[i].replace( _float, '' );
        }

        direction = direction[0];

        if( direction == 'S' || direction == 'W' )
            dd *= -1;

        directions[ directions.length ] = direction;

        coords[ coords.length ] = dd;
        dd = pow = 0;

    } else {

        dd += ( parseFloat(parts[i]) / Math.pow( 60, pow++ ) );

    }

}

if( directions[0] == 'W' || directions[0] == 'E' ) {
    var tmp = coords[0];
    coords[0] = coords[1];
    coords[1] = tmp;
}

return coords;

}

此函数不处理所有类型的 lat / long 类型,但它处理以下格式:

-31,2222,21.99999
-31 13 13 13.75S, -31 13 13 13.75W
-31 13 13 13.75S -31 13 13 13.75W
-31 13 13 13.75W -31 13.75S
36°57'9" N 110°4'21" W
110°4'21" W 36°57'9"N

这是我需要的。

于 2013-09-04T15:34:57.577 回答
4

我在这个函数上有一些 NaN 并且需要这样做(不要问我为什么)

function ConvertDMSToDD(days, minutes, seconds, direction) {
    var dd = days + (minutes/60) + seconds/(60*60);
    dd = parseFloat(dd);
    if (direction == "S" || direction == "W") {
        dd *= -1;
    } // Don't do anything for N or E
    return dd;
}
于 2010-08-06T06:45:16.277 回答
4

我使用了\d+(\,\d+)\d+(.\d+)因为可以有浮点数

我的最终功能:

 convertDMSToDD: function (dms) {
     let parts = dms.split(/[^\d+(\,\d+)\d+(\.\d+)?\w]+/);
     let degrees = parseFloat(parts[0]);
     let minutes = parseFloat(parts[1]);
     let seconds = parseFloat(parts[2].replace(',','.'));
     let direction = parts[3];

     console.log('degrees: '+degrees)
     console.log('minutes: '+minutes)
     console.log('seconds: '+seconds)
     console.log('direction: '+direction)

     let 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;
   }
于 2017-10-27T13:50:41.627 回答
3

乔,你提到的脚本已经做了你想要的。有了它,您可以转换 lat 和 long 并将其放入链接以查看 Google 地图中的位置:

var url = "http://maps.google.com/maps?f=q&source=s_q&q=&vps=3&jsv=166d&sll=" + lat.parseDeg() + "," + longt.parseDeg()
于 2009-07-16T21:23:42.677 回答