Plus codes

node v8.17.0
version: master
endpointsharetweet
A plus code is an address for any place on the earth. An example would be the code '9FFW84J9+XG' that describes the address to the Museum of Technology outside of Stockholm. This works by converting between coordinates described by longitude and latitude to a code where every other character describes either longitude or latitude in base 20. Read more about plus codes at https://plus.codes/. The NPM package https://www.npmjs.com/package/pluscodes is a Javascript implementation of plus codes, that works offline. To decode the plus code mentioned above -
const { decode } = require('pluscodes@2.5.0') decode('9FFW84J9+XG')
While the result above is represented as a map, since Runkit knows how to display coordinates like a map, you may choose "Properties Viewer" to inspect the data object returned from decode. It's a longitude and latitude pair, plus a resolution. A plus code doesn't represent a point, but rather an area. The resolution is the width and the height of that area in degrees and the longitude and latitude points to the center of the area. A code may also have lower resolution, which means fewer characters in the code, such as by omitting the last two characters -
decode('9FFW84J9+')
Or even ommitting more characters, which then have to be replaced by zeroes -
decode('9FFW0000+')
You can also use the NPM library to generate plus codes with the encode function and sending in the coordinates, such as -
const { encode } = require('pluscodes@2.5.0') encode({ latitude: '59.332438', longitude: '18.118813' })
You may also reduce the resolution of the code, by adding a length. By default the code contains 10 characters, not counting the plus sign. You may provide an alternative length, such as -
encode({ latitude: '59.332438', longitude: '18.118813' }, 8)
encode({ latitude: '59.332438', longitude: '18.118813' }, 4)
You may also shorten a plus code by removing characters from the left, such as '84J9+XG'. A code like that cannot be decoded on its own, it needs a reference point. In comparision, a normal street address doesn't always have to contain the country, if that can be deduced by other means. In a similar fashion, if a short code is shown on a sign, the current position can be used as a refence. Another way is by attaching a position, such as the name of a city and/or country, to the short code, like '84J9+XG Stockholm'. To shorten a plus code, you may use the shorten function, where the reference in this case points to Stockholm -
const { shorten } = require('pluscodes@2.5.0') shorten('9FFW84J9+XG', { latitude: '59.329394', longitude: '18.068712' })
To expand a shortened code back, you may use the expand function -
const { expand } = require('pluscodes@2.5.0') expand('84J9+XG', { latitude: '59.329394', longitude: '18.068712' })
While it needs a network connection, a library such as node-geocoder may be used the find the reference point, based on a name, such as -
const NodeGeocoder = require('node-geocoder') var geocoder = NodeGeocoder({ provider: 'openstreetmap' }) geocoder.geocode('Stockholm, Sweden').then(places => expand('84J9+XG', places[0])).then(console.log) && "Fetching geocode..."
Loading…

no comments

    sign in to comment