Skip to content

Instantly share code, notes, and snippets.

@tjunxiang92
Created April 21, 2017 21:19
Show Gist options
  • Save tjunxiang92/1050bb03af2801e853a082ee8513b90e to your computer and use it in GitHub Desktop.
Save tjunxiang92/1050bb03af2801e853a082ee8513b90e to your computer and use it in GitHub Desktop.
Translate Address into lat lng
// Geocoding
// https://developers.google.com/maps/documentation/geocoding/intro
const API_KEY = "Fetch from google: https://developers.google.com/maps/documentation/javascript/get-api-key";
const address = "Here is your address";
const decodeLatLng = address => {
return new Promise((resolve, reject) => {
request(`https://maps.googleapis.com/maps/api/geocode/json?address=${address}&key=${API_KEY}`,
(error, response, body) => {
if (error) reject(error);
let decoded = JSON.parse(body);
let latlng = decoded.results[0].geometry.location;
resolve(latlng);
});
});
};
// Prints out the decoded latlng
decodeLatLng(address).then(latlng => console.log(latlng);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment