Last active
May 3, 2017 15:20
-
-
Save mjrdnk/8104f7ee94e023eb6a4cbcadd29b86b8 to your computer and use it in GitHub Desktop.
Reverse Google Maps geocoding with geohash for area of 3-letter hash accuracy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let NodeGeocoder = require('node-geocoder'); | |
let geohash = require('latlon-geohash'); | |
const options = { | |
provider: 'google', | |
httpAdapter: 'https', | |
apiKey: process.env.GOOGLE_MAPS_KEY, | |
formatter: null | |
}; | |
let geocoder = NodeGeocoder(options); | |
let getCoordinates = (req, res) => { | |
let location = req.params.location; | |
geocoder.geocode(location).then((response) => { | |
insertGeohashInResponse(response); | |
res.json(response); | |
}).catch((err) => { | |
console.log(err); | |
}); | |
} | |
let insertGeohashInResponse = (response) => { | |
let accurateHash = ''; | |
let shorterHash = ''; | |
for (var i = 0; i < response.length; i++) { | |
accurateHash = geohash.encode(response[i].latitude, response[i].longitude); | |
// shorterHash represents bigger area | |
shorterHash = accurateHash.slice(0, 3); | |
// inserts geohash in the response | |
response[i].geohash = (lat && lon) ? shorterHash : null; | |
} | |
} | |
module.exports = { | |
getCoordinates | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment