Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Latitude/Longitude DMS to Decimal Converter

    Converts Latitude and Longitude by DMS (Degrees, Minutes, Seconds) into decimal format. Please let me know if you find any bugs. I know that you can put up to 99 minutes and seconds in the fields, but that should still result in the correct decimal format regardless, as far as I have tested it.

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
    <head>
         <title>Latitude/Longitude DMS to Decimal Converter</title>
    <script type="text/javascript">
    <!--
    /******************************************
    DMS to Decimal Latitude/Longitude Converter
    © 2002 Robert K. Davis
    *******************************************/
    function convert(D,M,S){
         var DD;
         D < 0 ? DD = roundOff(D + (M/-60) + (S/-3600),6) : DD = roundOff(D + (M/60) + (S/3600),6);
         return DD;
    }
    function roundOff(num,decimalplaces){
         var decimalfactor = Math.pow(10,decimalplaces);
         var roundedValue = Math.round(num*decimalfactor)/decimalfactor;
         return roundedValue;
    }
    function toDecimal(f){
         var LatDegrees = parseInt(f.LatDegrees.value);
         var LatMinutes = parseInt(f.LatMinutes.value);
         var LatSeconds = parseInt(f.LatSeconds.value);
         var LonDegrees = parseInt(f.LonDegrees.value);
         var LonMinutes = parseInt(f.LonMinutes.value);
         var LonSeconds = parseInt(f.LonSeconds.value);
    
         var LatDecimalDegrees = convert(LatDegrees,LatMinutes,LatSeconds);
         var LonDecimalDegrees = convert(LonDegrees,LonMinutes,LonSeconds);
    
         !isNaN(LatDecimalDegrees) && !(LatDecimalDegrees > 90) && !(LatDecimalDegrees < -90) ? f.LatDecimalDegrees.value = LatDecimalDegrees : f.LatDecimalDegrees.value = "";
         !isNaN(LonDecimalDegrees) && !(LonDecimalDegrees > 180) && !(LonDecimalDegrees < -180)  ? f.LonDecimalDegrees.value = LonDecimalDegrees : f.LonDecimalDegrees.value = "";
    }
    // -->
    </script>
    </head>
    <body>
         <form id="conversionform" action="javascript://">
              <table border="0" cellspacing="0" cellpadding="0" width="200">
                   <tr valign="top">
                        <td>Latitude:&nbsp;</td>
                        <td><input type="text" name="LatDegrees" size="3" value="" onblur="this.value=this.value.replace(/[^\d\-]/g,'')" />&deg;</td>
                        <td><input type="text" name="LatMinutes" size="3" value="" onblur="this.value=this.value.slice(0,2).replace(/\D/g,'')"/>&#39;</td>
                        <td><input type="text" name="LatSeconds" size="3" value="" onblur="this.value=this.value.slice(0,2).replace(/\D/g,'')"/>&quot;</td>
                   </tr>
                   <tr valign="top">
                        <td>Longitude:&nbsp;</td>
                        <td><input type="text" name="LonDegrees" size="3" value="" onblur="this.value=this.value.replace(/[^\d\-]/g,'')"/>&deg;</td>
                        <td><input type="text" name="LonMinutes" size="3" value="" onblur="this.value=this.value.slice(0,2).replace(/\D/g,'')"/>&#39;</td>
                        <td><input type="text" name="LonSeconds" size="3" value="" onblur="this.value=this.value.slice(0,2).replace(/\D/g,'')"/>&quot;</td>
                   </tr>
                   <tr valign="top">
                        <td colspan="4">&nbsp;</td>
                   </tr>
                   <tr valign="top">
                        <td colspan="4" align="center"><input type="button" value="Convert" onclick="toDecimal(this.form)" /></td>
                   </tr>
                   <tr valign="top">
                        <td colspan="4">&nbsp;</td>
                   </tr>
                   <tr valign="top">
                        <td>Latitude:&nbsp;</td><td colspan="3"><input type="text" name="LatDecimalDegrees" value="" /></td>
                   </tr>
                   <tr valign="top">
                        <td>Longitude:&nbsp;</td><td colspan="3"><input type="text" name="LonDecimalDegrees" value="" /></td>
                   </tr>
              </table>
         </form>
    </body>
    </html>
    Last edited by whammy; 11-13-2002 at 01:26 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •