How to convert GPS degree to decimal and vice versa in jquery or javascript and PHP?

Does anyone know how to convert GPS degree to decimal values ​​or vice versa?

I need to develop a way by which users can insert an address and get GPS values ​​(both degrees and decimal), but the main thing I need to know is how to convert the values, force users to also insert GPS values ​​(degrees or decimal numbers) ) Since I need to get a map from Google maps, this requires a decimal number.

I tried some codes, but I get large numbers ... like this:

function ConvertDMSToDD(days, minutes, seconds, direction) { var dd = days + minutes/60 + seconds/(60*60); //alert(dd); if (direction == "S" || direction == "W") { dd = '-' + dd; } // Don't do anything for N or E return dd; } 

Any?

Thanks.

+5
source share
2 answers

Thanks first to @Eugen Rieck for your help. Here is my last code, hope it can help someone:

degree to decimal

 function getDMS2DD(days, minutes, seconds, direction) { direction.toUpperCase(); var dd = days + minutes/60 + seconds/(60*60); //alert(dd); if (direction == "S" || direction == "W") { dd = dd*-1; } // Don't do anything for N or E return dd; } 

decimal to the extent based on this link

 function getDD2DMS(dms, type){ var sign = 1, Abs=0; var days, minutes, secounds, direction; if(dms < 0) { sign = -1; } Abs = Math.abs( Math.round(dms * 1000000.)); //Math.round is used to eliminate the small error caused by rounding in the computer: //eg 0.2 is not the same as 0.20000000000284 //Error checks if(type == "lat" && Abs > (90 * 1000000)){ //alert(" Degrees Latitude must be in the range of -90. to 90. "); return false; } else if(type == "lon" && Abs > (180 * 1000000)){ //alert(" Degrees Longitude must be in the range of -180 to 180. "); return false; } days = Math.floor(Abs / 1000000); minutes = Math.floor(((Abs/1000000) - days) * 60); secounds = ( Math.floor((( ((Abs/1000000) - days) * 60) - minutes) * 100000) *60/100000 ).toFixed(); days = days * sign; if(type == 'lat') direction = days<0 ? 'S' : 'N'; if(type == 'lon') direction = days<0 ? 'W' : 'E'; //else return value return (days * sign) + 'ΒΊ ' + minutes + "' " + secounds + "'' " + direction; } alert(getDD2DMS(-8.68388888888889, 'lon')); 

`

+8
source

I could not get the script to work above, after a while I came up with this; just give dms function

 function ConvertDMSToDEG(dms) { var dms_Array = dms.split(/[^\d\w\.]+/); var degrees = dms_Array[0]; var minutes = dms_Array[1]; var seconds = dms_Array[2]; var direction = dms_Array[3]; var deg = (Number(degrees) + Number(minutes)/60 + Number(seconds)/3600).toFixed(6); if (direction == "S" || direction == "W") { deg = deg * -1; } // Don't do anything for N or E return deg; } 

and vice versa, just give degrees to the script, and true for false for latitude (latitude)

 function ConvertDEGToDMS(deg, lat) { var absolute = Math.abs(deg); var degrees = Math.floor(absolute); var minutesNotTruncated = (absolute - degrees) * 60; var minutes = Math.floor(minutesNotTruncated); var seconds = ((minutesNotTruncated - minutes) * 60).toFixed(2); if (lat) { var direction = deg >= 0 ? "N" : "S"; } else { var direction = deg >= 0 ? "E" : "W"; } return degrees + "Β°" + minutes + "'" + seconds + "\"" + direction; } 

hope this helps people ..

0
source

Source: https://habr.com/ru/post/1388577/


All Articles