Last active
February 2, 2021 10:35
-
-
Save nathanjohnson320/7283784 to your computer and use it in GitHub Desktop.
Use the Google Places API with node.js
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
exports.randeats = function(req, res){ | |
var key = req.query.key; | |
var location = encodeURIComponent(req.query.location); | |
var radius = 16000; | |
var sensor = false; | |
var types = "restaurant"; | |
var keyword = "fast"; | |
var https = require('https'); | |
var url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" + "key=" + key + "&location=" + location + "&radius=" + radius + "&sensor=" + sensor + "&types=" + types + "&keyword=" + keyword; | |
console.log(url); | |
https.get(url, function(response) { | |
var body =''; | |
response.on('data', function(chunk) { | |
body += chunk; | |
}); | |
response.on('end', function() { | |
var places = JSON.parse(body); | |
var locations = places.results; | |
var randLoc = locations[Math.floor(Math.random() * locations.length)]; | |
res.json(randLoc); | |
}); | |
}).on('error', function(e) { | |
console.log("Got error: " + e.message); | |
}); | |
}; |
Thanks, I'm Happy
Thankyou so much for this!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am not getting any reaponse while using above code. can you help me with that?