geocode
Geocodes a lat/lng.
Setup:
npm install codeblox @google/maps
.codebloxenv
GOOGLE_API_KEY=YOUR_KEY_HERE
codeblox.json
{
"name": "geocode",
"version": "1.0.0",
"description": "Geocodes an address",
"keywords": [
"geocode",
"address",
"latitude",
"longitude"
],
"directions": "Enter a city (New York, Paris, etc...)",
"input": "text",
"output": "location"
}
codeblox.js
const codeblox = require('codeblox')
const googleMapsClient = require('@google/maps').createClient({
key: process.env.GOOGLE_API_KEY
})
module.exports.default = codeblox((input, options, callback) => {
googleMapsClient.geocode({
address: input
}, (err, response) => {
if (!err) {
if (response.json.results.length) {
const lat = response.json.results[0].geometry.location.lat;
const lng = response.json.results[0].geometry.location.lng;
return callback(null, [lat, lng])
}
return callback('Location not found')
}
callback(err)
});
})