use: Node.js Request, to: query googleMaps' API via HTTP
Last active
March 9, 2016 16:40
-
-
Save kmassada/3040b9224a2851944b9a to your computer and use it in GitHub Desktop.
Node + Request + googleMaps API HTTP
This file contains hidden or 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
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); | |
}); |
This file contains hidden or 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
// 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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment