5 Replies Latest reply on Sep 9, 2010 5:19 PM by George_Johnson

    Custom Text Field Format for Lat/Long

    phil.selmer

      I'm new to creating PDFs and haven't done JavaScript for 10+ years.

       

      I was hoping someone would be able to provide the custom format script required toformat a text field for Lat/Long in DMS (ex. 70° 12' 34" W).

       

      Short of that, does anyone have a custom format script I could start from and modify?

       

      Thanks in advance!

        • 1. Re: Custom Text Field Format for Lat/Long
          George_Johnson MVP & Adobe Community Professional

          That's possible, but what would a user enter into the field in your example?

          • 2. Re: Custom Text Field Format for Lat/Long
            phil.selmer Level 1

            User would enter in decimal format (ie. 12.3456 or -45.6789)

            • 3. Re: Custom Text Field Format for Lat/Long
              George_Johnson MVP & Adobe Community Professional

              I created a conversion routine a while ago, but don't have access to it at the moment. It could easily be adapted to a format script. I should be able to post something later tonight.

              • 4. Re: Custom Text Field Format for Lat/Long
                phil.selmer Level 1

                Thanks very much!  I'll be looking for your response.

                • 5. Re: Custom Text Field Format for Lat/Long
                  George_Johnson MVP & Adobe Community Professional

                  Try calling the function shown below in the latitude or longitude field's custom Format script. This function is intended to be placed as a document-level script.

                   

                  Use the following as the text field's custom Format script:

                   

                  // For Longitude field

                  dd_to_dms(true);

                   

                  -OR-

                   

                  //For Latitude field

                  dd_to_dms(false);

                   

                  I was liberal with the comments in hopes of making it clear. What I did not do is any sort of data validation. The field entry is assumed to be a number that makes sense (e.g., within +/- 180 for longitude, +/- 90 for latitude). You could add a custom Keystroke script to limit the entry to a number and a Validation script to check its value. It also rounds the seconds down to the nearest second, but this could be changed if you need a fractional part for the seconds. Post again if you have any questions.

                   

                   

                   

                  function dd_to_dms(isLong) {

                   

                      // Custom Format script for acroform text field
                      // Convert lat/long value from decimal to Degrees, Minutes, Seconds
                      // ddd.dddd -> [DD]D° [M]M' [S]S" N|S|E|W
                      // isLong: false = latitude, true = longitude

                   

                      // Blank field if no entry
                      if (!event.value) {
                          event.value = "";
                          return;
                      }

                   

                      // Get field entry as a number
                      var val = +event.value;

                   

                      var cd, t, degrees, minutes, seconds;

                   

                      // Determine which cardinal to display
                      if (isLong) {
                          cd = (val < 0) ? "W" : "E";
                      } else {
                          cd = (val < 0) ? "S" : "N";
                      }

                   

                      // Make number positive
                      val = Math.abs(val);

                   

                      // degrees = integer portion of input value
                      degrees = val | 0;

                   

                      // Get fractional part
                      t = val % 1.0;

                   

                      // minutes = integer portion of (fractional part * 60)
                      t = t * 60;
                      minutes = t | 0;

                   

                      // seconds = integer portion of (previous fractional part * 60)
                      t = t % 1.0;
                      seconds = (t * 60) | 0;

                   

                      // Display in field
                      event.value = degrees + "\u00b0 " + minutes + "' " + seconds + "\" " + cd;
                  }