Skip to content

Instantly share code, notes, and snippets.

@Abuwabu
Last active May 3, 2016 12:18
Show Gist options
  • Save Abuwabu/07c2fec4dc5e970f49ac to your computer and use it in GitHub Desktop.
Save Abuwabu/07c2fec4dc5e970f49ac to your computer and use it in GitHub Desktop.
Request ip from ipinfo.io If no city, determine from google via Lat&Lon
var request = require('request'),
url = 'http://ipinfo.io',
google = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='
module.exports = function () {
return new Promise(function (resolve, reject) {
request({
url: url,
json: true
}, function (error, response, body) {
if (error) {
reject('Unable to guess location!');
} else if (body.city.length === 0) {
var latLon = body.loc;
request({
url: google + latLon,
json: true
}, function (error, response, body) {
if (error) {
reject('Unable to decifer location from LatLon');
} else {
body.city = body.results[0].formatted_address;
resolve(body);
}
});
} else {
reject('Unable to guess location!!');
}
});
});
};
@saumyasuhagiya
Copy link

@Abuwabu: in case location is detected fine, it will go in else by default, it should be changed with one default option as below
if(body.city.legth != 0) {
resolve(body);
}

or something similar.

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