Convert latitude and longitude to decimal values

I have GPS information presented in the form:

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

I can use the Chris Veness javascript functions to convert degrees, minutes and seconds to numerical degrees, but first I need to parse the GPS information into separate lines of latitude and longitude (with NSEW suffixes). I read related stackoverflow posts, but I'm not a regular expression expert (or programmer) and don't need some help with the parsing function. What is the best way to parse this string in latitude and longitude for use in a transform function?

All this will result in a web link that you can click to see a view of the location of the Google map.

+20
javascript regex geolocation geocoding gps
Jul 16 '09 at 20:44
source share
7 answers

Use the following to analyze your input.

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

Thus, your input will result in the following:

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

Decimal coordinates can be sent to Google maps for points through GLatLng(lat, lng) ( Google Maps API )

+40
Jul 16 '09 at 21:12
source share

The above functions are fixed and the object is concluded.

 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; } 
+9
Oct. 22 '15 at 12:00
source share

My modified version forces strings to Numbers so they can be added together rather than concatenated. It also processes decimal values ​​that are common to the Seconds component:

 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 = 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; } 
+4
Sep 09 '15 at 15:52
source share

I have NaN for this function and need to do it (don't ask me why)

 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; } 
+3
Aug 6 '10 at 6:45
source share

Joe, the script you mentioned is what you need. With it, you can convert lat and long and put it in a link to see the location on a Google map:

 var url = "http://maps.google.com/maps?f=q&source=s_q&q=&vps=3&jsv=166d&sll=" + lat.parseDeg() + "," + longt.parseDeg() 
+2
Jul 16 '09 at 21:23
source share

here is my example:

 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; 

}

This function does not process all lat / long types, but it processes the following formats:

 -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 

This is what I need.

+2
04 Sep '13 at 15:34
source share

I used \ d + (\, \ d +) and \ d + (. \ D +) since they can have floating point numbers

My final function:

  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; } 
0
Oct 27 '17 at 13:50
source share



All Articles