Source Code for JavaScript Mapping Tools: formatters.js

Source code of a graphical tool for drawing and computing distances over Google maps.

Run Tool | index.html | main.css | formatters.js | geoCircle.js | geoCode.js | geo.js | index.js | mapControls.js | tableManager.js | util.js | wayPoint.js | wayPointsManager.js


// Copyright 2006-2008 (c) Paul Demers  <paul@acscdg.com>

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA., or visit one 
// of the links here:
//  http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
//  http://www.acscdg.com/LICENSE.txt

//////////////////////////////////////////////////////////////////

// Formatting methods for  map drawing and distance tools.
// Dependency on other modules:
//  Google maps (for geo point object).

// Web site with this code running: http://www.acscdg.com/

//
//// Formats a positive real number to at least 2 significant digits.
function formatFloat(number)
{
  // Assumes number > 0.
  var result;
  if (number < 1)
  {
     var temp = Math.round(number * 100) / 100;
     result = temp.toString();
  }
  else if (number < 10)
  {
    var temp = Math.round(number * 10) / 10;
    result = temp.toString();
  }
  else
  {
    result = Math.round(number);
  }

  return result;
}


//
//// Zero fills numbers less than 10 to 2 digits.
function format10(number)
{
   var result;
   if (number >= 10)
     result = "" + number;
   else
     result = "0" + number;

   return result;
}

//
//// Zero fills numbers less than 100 to 3 digits.
function format100(number)
{
   var result;
   if (number >= 100)
     result = "" + number;
   else if (number >= 10)
     result = "0" + number;
   else
     result = "00" + number;

   return result;
}


//
//// Formats the latigude in the given point object to: 
////   DD MM SS d (zero filled).
function myLatToString(point)
{
   var lat = point.lat();
   var latDir;
   if (lat < 0)
   {
     latDir = "S";
     lat = lat * -1.0;
   }
   else
   {
     latDir = "N";
   }

   var latDegInt = Math.floor(lat);
   var latMinInt = Math.floor((lat * 60.0) - (latDegInt * 60.0));
   var latSecInt = Math.floor((lat * 3600.0) - (latDegInt * 3600.0) - (latMinInt * 60.0));

   return format10(latDegInt) + " " + format10(latMinInt) + " " + format10(latSecInt) + " " + latDir;
}


//
//// Formats the longitude in the given point object to:
////   DDD MM SS d (zero filled)
function myLngToString(point)
{
   var lng = point.lng();
   var lngDir;
   if (lng < 0)
   {
     lngDir = "W";
     lng = lng * -1.0;
   }
   else
   {
     lngDir = "E";
   }

   var lngDegInt = Math.floor(lng);
   var lngMinInt = Math.floor((lng * 60.0) - (lngDegInt * 60.0));
   var lngSecInt = Math.floor((lng * 3600.0) - (lngDegInt * 3600.0) - (lngMinInt * 60.0));

   return format100(lngDegInt) + " " + format10(lngMinInt) + " " + format10(lngSecInt) + " " + lngDir;
}