To further extend the above answer. I have included an example that uses the geonames Node.JS module from Github. ( https://github.com/bugs181/geonames )
Disclaimer: I am the author of the aforementioned github repo.
This example can also be found on the Github repo.
var geonames = require('./geonames.js');
// Intialize geonames module.
geonames.init();
// 34.0500, -118.2500 - Los Angeles, California - More specifically Bunker Hill, California
// 34.057°N 118.238°W - San Francisco, California - More specifically Opera Plaza, California
geonames.city(34.0500, -118.2500, function(err, result) {
if (err) {
console.log("There was an error resolving coordinates.");
console.log(err);
return;
}
console.log("Result: " + JSON.stringify(result, true, 3));
});
Output:
[geonames] Resolve city from coordinates. [34.05, -118.25]
Result: {
"city": "Bunker Hill",
"state": "CA"
}
You may use strings for the latitude and longitude as well but you must convert them for the Mongo database. Use the Number()
method to do this.
For example:
var lat = "34.0500";
var lon = "-118.2500";
geonames.city(Number( lat ), Number( lon ), function(err, result) {