Skip to content

Instantly share code, notes, and snippets.

@dougmulley
Created August 9, 2022 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougmulley/d73ccd556da649d798963130b1cdb759 to your computer and use it in GitHub Desktop.
Save dougmulley/d73ccd556da649d798963130b1cdb759 to your computer and use it in GitHub Desktop.
Convert 4- or 6-character Maidenhead grid square to latitude and longitude
#!/usr/bin/env node
// Usage: maidenhead-to-lat_long <maidenhead-grid-square>
// Credit: Using the conversion method describe here at
// https://www.m0nwk.co.uk/how-to-convert-maidenhead-locator-to-latitude-and-longitude/
const mhGridSquare = process.argv[2];
console.log(`Maidenhead grid square: ${mhGridSquare}`);
const latitudePart1 = (mhGridSquare.charCodeAt(1) - 65) * 10;
const latitudePart2 = parseInt(mhGridSquare[3]);
const latitudePart3 = mhGridSquare.length > 4
? (((mhGridSquare.charCodeAt(5) - 97) / 24.0) + (1.0 / 48.0))
: 0;
const latitude = latitudePart1 + latitudePart2 + latitudePart3 - 90;
console.log(`Latitude: ${latitude}`);
const longitudePart1 = (mhGridSquare.charCodeAt(0) - 65) * 20;
const longitudePart2 = mhGridSquare[2] * 2;
const longitudePart3 = mhGridSquare.length > 4
? ((mhGridSquare.charCodeAt(4) - 97) / 12.0) + ( 1.0 / 24.0)
: 0;
const longitude = longitudePart1 + longitudePart2 + longitudePart3 - 180;
console.log(`Longitude: ${longitude}`);
console.log(`Latitude, longitude: ${latitude}, ${longitude}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment