if you Need an API to fetch countries, states, regions, cities dynamically here's the library you're looking for
geonames.js is a flexible library for browser and Nodejs built on top geonames.org REST api
npm install --save geonames.js
You have to register (it's free) on Geonames.org in order to get the username that will be necessary for the api to work
You can fetch almost anything taking advandage of the huge amount of information provided by geonames.org, It contains over 10 million geographical names and consists of over 9 million unique features whereof 2.8 million populated places and 5.5 million alternate names.
The list of available API is in here under the webservice column.
-
Nodejs: (bluebird promise based API)
var Geonames = require('geonames.js'); geonames = new Geonames({username: 'myusername', lan: 'en', encoding: 'JSON'});
//plain call geonames.search({q: 'CONT'}) //get continents .then(function(resp){ console.log(resp.geonames); }) .catch(function(err){ })
//chaining calls geonames.countryInfo({}) .then(function(countries){ return geonames.children({geonameId: countries.geonames[0].geonameId}) }) .then(function(states){ return geonames.children({geonameId: stateOrProvince.geonames[0].geonameId}); }) .then(function(regions){ return geonames.children({geonameId: region.geonames[0].geonameId}); }) .then(function(cities){ console.log(cities.geonames); }) .catch(function(err){ })
-
browser: (plain ajax xhr call)
import the script:
<script type="text/javascript" src="node_modules/geonames.js/dist/geonames.min.js"></script>
//GeoNames constructor is attacched to the global object
geonames = new GeoNames({username: username, lan: 'en', encoding: 'JSON'});
geonames.search({q: 'CONT'}, function(continents){ //plain xhr call
console.log(continents);
}, function(err){
//error
})
Feel free to contribute, any help is really appreciated :)
run with:
gulp
gulp dist (for the minification)
USERNAME=myusername npm test (for unit testing with mocha)
MIT License
Copyright (c) 2017 kinotto
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.