JScoord

Easily convert between latitude/longitude, Universal Transverse Mercator (UTM) and Ordnance Survey (OSGB) references with JavaScript using the JScoord package.
Latest version: 1.1.1 (16th January 2006)

Features | Download | Examples | Version History | Licensing

Conversion between latitude/longitude and grid references is not as simple as one would expect. Indeed, the maths involved is rather complex and not really for the faint-hearted! I have created a JavaScript script which provides a simple API to allow conversion between OSGB (Ordnance Survey of Great Britain) grid references, UTM (Universal Transverse Mercator) references and latitude/longitude. OSGB Grid references can be returned as a standard six-figure grid reference using the 100km grid square letters (e.g. SK619847).

The JScoord script also contains a function to allow the calculation of the surface distance between two points of latitude/longitude.

Looking for the same thing but with PHP or Java? Translations of this package with the same functionality are available for PHP (see PHPcoord) and Java (see Jcoord).

Information on converting between UTM and latitude/longitude is available at www.posc.org.

The Ordnance Survey have published a concise guide on converting between OSGB and latitude/longitude which is available at www.gps.gov.uk.

Bear in mind that JScoord is still in development, so check back often for updates. To contact me about JScoord, send an e-mail to jscoord@jstott.me.uk.

Features

JScoord contains the following features:

Download

Current Version

Previous Versions

Examples

Calculate Surface Distance between two Latitudes/Longitudes

The distance() function takes a reference to a LatLng object as a parameter and calculates the surface distance between the the given object and this object in kilometres:

var lld1 = new LatLng(40.718119, -73.995667); // New York
document.write("New York Lat/Long: " + lld1.toString() + "<br />");
var lld2 = new LatLng(51.499981, -0.125313);  // London
document.write("London Lat/Long: " + lld2.toString() + "<br />");
var d = lld1.distance(lld2);
document.write("Surface Distance between New York and London: " + d + "km");

produces the following output:

Convert OS Grid Reference to Latitude/Longitude

Note that the OSGB-Latitude/Longitude conversions use the OSGB36 datum by default. The majority of applications use the WGS84 datum, for which the appropriate conversions need to be added. See the examples below to see the difference between the two data.

Using OSGB36 (convert an OSGB grid reference to a latitude and longitude using the OSGB36 datum):

var os1 = new OSRef(651409.903, 313177.270);
document.write("OS Grid Reference: " + os1.toString() + " - " + os1.toSixFigureString() + "<br />");
var ll1 = os1.toLatLng();
document.write("Converted to Lat/Long: " + ll1.toString());

produces the following output:

Using WGS84 (convert an OSGB grid reference to a latitude and longitude using the WGS84 datum):

var os1w = new OSRef(651409.903, 313177.270);
document.write("OS Grid Reference: " + os1w.toString() + " - " + os1w.toSixFigureString() + "<br />");
var ll1w = os1w.toLatLng(os1w);
ll1w.OSGB36ToWGS84();
document.write("Converted to Lat/Long: " + ll1w.toString());

produces the following output:

Convert Latitude/Longitude to OS Grid Reference

Note that the OSGB-Latitude/Longitude conversions use the OSGB36 datum by default. The majority of applications use the WGS84 datum, for which the appropriate conversions need to be added. See the examples below to see the difference between the two data.

Using OSGB36 (convert a latitude and longitude using the OSGB36 datum to an OSGB grid reference):

var ll2 = new LatLng(52.657570301933, 1.7179215806451);
document.write("Latitude/Longitude: " + ll2.toString() + "<br />");
var os2 = ll2.toOSRef();
document.write("Converted to OS Grid Ref: " + os2.toString() + " - " + os2.toSixFigureString());

produces the following output:

Using WGS84 (convert a latitude and longitude using the WGS84 datum to an OSGB grid reference):

var ll2w = new LatLng(52.657570301933, 1.7179215806451);
document.write("Latitude/Longitude: " + ll2.toString() + "<br />");
ll2w.WGS84ToOSGB36();
var os2w = ll2w.toOSRef();
document.write("Converted to OS Grid Ref: " + os2w.toString() + " - " + os2w.toSixFigureString());

produces the following output:

Convert Six-Figure OS Grid Reference String to an OSRef Object

To convert a string representing a six-figure OSGB grid reference:

var os6 = "TG514131";
document.write("Six figure string: " + os6 + "<br />");
var os6x = getOSRefFromSixFigureReference(os6);
document.write("Converted to OS Grid Ref: " + os6x.toString() + " - " + os6x.toSixFigureString());

produces the following output:

Convert UTM Reference to Latitude/Longitude

var utm1 = new UTMRef(456463.99, 3335334.05, "E", 12);
document.write("UTM Reference: " + utm1.toString() + "<br />");
var ll3 = utm1.toLatLng();
document.write("Converted to Lat/Long: " + ll3.toString());

produces the following output:

Convert Latitude/Longitude to UTM Reference

var ll4 = new LatLng(-60.1167, -111.7833);
document.write("Latitude/Longitude: " + ll4.toString() + "<br />");
var utm2 = ll4.toUTMRef();
document.write("Converted to UTM Ref: " + utm2.toString());

produces the following output:

Convert a LatLng object for use with the Google Maps API GLatLng

var ll = new LatLng(-60.1167, -111.7833);
var llg = new GLatLng(ll.lat, ll.lng);
  

Version History

Licensing

This software product is available under the GNU General Public License (GPL) which permits the use of this product subject to a number of conditions as described in the license. A commercial license is also available for this product which provides the added benefits of royalty-free use and distribution in proprietary applications as well as prioritised technical support by e-mail. Contact me for more information about commercial licenses for this product.

Solution Graphics

If you do not require a commercial license but still want to contribute, perhaps you would like to consider making a donation:

Commercial licenses for this product are available at the following prices:

Note that you will be billed in Pounds Sterling, so the relevant exchange rate from your currency will apply at the time of purchase. A copy of the license will be forwarded to the address provided at the time of purchase.

Save up to $90 with Dreamhost