Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-13-2002, 12:15 AM   PM User | #1
whammy
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
whammy is an unknown quantity at this point
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,'')"/>'</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,'')"/>'</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>
__________________
Former ASP Forum Moderator - I'm back!

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

Last edited by whammy; 11-13-2002 at 12:26 AM..
whammy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:03 AM.


Advertisement
Log in to turn off these ads.