Skip to content

Instantly share code, notes, and snippets.

@emersonbroga emersonbroga/README.md
Last active Nov 25, 2018

Embed
What would you like to do?
Coordinates from adrress with NodeJs and Google Maps
const request = require('request');
const API_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
const API_KEY = '';
const parseJson = (string) => {
try{
return JSON.parse(string);
}catch(e) {
return null;
}
}
const doRequest = (url) =>{
const promisseCallback = (resolve, reject) => {
request(url, (error, response, body) => {
if (error) reject(error);
const data = parseJson(body);
resolve(data);
});
};
return new Promise(promisseCallback);
}
const getApiUrl = (address) => {
return `${API_URL}?key=${API_KEY}&address=${encodeURI(address)}`;
}
const address = '368 Broadway, New York, NY 10013, USA';
(async () => {
const apiUrl = getApiUrl(address);
const data = await doRequest(apiUrl);
if (!data || data.error_message) {
const message = (data && data.error_message) ? data.error_message : 'Api Error';
console.log(message);
return;
}
console.log(data.results[0].geometry.location);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.