Skip to content

Instantly share code, notes, and snippets.

@kmassada
Last active March 9, 2016 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmassada/3040b9224a2851944b9a to your computer and use it in GitHub Desktop.
Save kmassada/3040b9224a2851944b9a to your computer and use it in GitHub Desktop.
Node + Request + googleMaps API HTTP
var GoogleMapsClientLib = require('../services/GoogleMapsClient');
var GoogleMapsClient = new GoogleMapsClientLib();
GoogleMapsClient.placeSearch(req.body, function(err, places, info) {
if (err) {
return next(err);
}
if (!places) {
res.status(500);
return res.send({error: info});
}
return res.json(places);
});
// Constructor
var GoogleMapsClient = function GoogleMapsClient(request) {
this.request = request;
this.apiKey = process.env.GOOGLE_MAPS_API;
};
GoogleMapsClient.prototype.placeSearch = function(queryString, done) {
// Qs: {location: '-33.8670,151.1957',
// radius: 500,
// types: 'food',
// name: 'cruise',}
if (queryString) {queryString.key = apiKey;}
request({
method: 'GET',
url: 'https://maps.googleapis.com/maps/api/place/nearbysearch/json',
qs: queryString,
json: true,
}, function(err, httpResponse, body) {
if (err) {
return done(err);
}
if (body.status == 'ZERO_RESULTS' || !body.results) {
return done(null, false, 'Not found.');
}
if (body.status == 'REQUEST_DENIED') {
return done(null, false, 'Not authorized to use this API.');
}
return done(null, body.results);
});
};
GoogleMapsClient.prototype.geoCode = function(queryString, done) {
// {address: '500 president street'},
if (queryString) {queryString.key = apiKey;}
request({
method: 'GET',
url: 'https://maps.googleapis.com/maps/api/geocode/json',
qs: queryString,
json: true,
}, function(err, httpResponse, body) {
if (err) {
return done(err);
}
if (body.status == 'ZERO_RESULTS' || !body.results) {
return done(null, false, 'Not found.');
}
if (body.status == 'REQUEST_DENIED') {
return done(null, false, 'Not authorized to use this API.');
}
return done(null, body);
});
};
module.exports = GoogleMapsClient;

use: Node.js Request, to: query googleMaps' API via HTTP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment