Last active
May 3, 2016 12:18
-
-
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
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 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!!'); | |
} | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.