Giter Club home page Giter Club logo

geonames.js's Introduction

geonames.js v3.0.3 NEW (see changelog)

geonames.js is a flexible library for browser and Nodejs built on top geonames.org webservices.

It provides a simple API to dynamically fetch countries, states, regions, cities and other milion geographical names.

not found not found not found

img



1. Installation

npm install --save geonames.js

or

yarn add geonames.js

2. Requirements

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.

geonames.js depends on a native ES6 Promise implementation to be supported. If your environment doesn't support ES6 Promises, you can use a polyfill.

3. Usage:

You can fetch almost anything by taking advantage 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 options in the API is in here under the webservice column.

  • Import the library:

    • server usage (NodeJS)
       const { Geonames } = require('geonames.js') /* commonJS */
    • browser usage (React, Angular, Vue etc.)
       import Geonames from 'geonames.js'; /* es module */
  • Usage:

    Initialize the Geoames using your settings:

    free WS

    const geonames = Geonames({
      username: 'myusername',
      lan: 'en',
      encoding: 'JSON'
    });

    commercial WS

    To use the commercial tier just define your token:

    const geonames = Geonames({
      username: 'myusername',
      token: 'mytoken',
      lan: 'en',
      encoding: 'JSON'
    });

    Since the library return promises, you can use either async/await or promise-based syntax

    plain call

    // async/await
    try{
      const continents = await geonames.search({q: 'CONT'}) //get continents
    }catch(err){
      console.error(err);
    }
    
    // promise
    geonames.search({q: 'CONT'}) //get continents
    .then(resp => {
      console.log(resp.geonames);
    })
    .catch(err => console.error(err));

    chaining calls

    // async/await
    try{
      const countries = await geonames.countryInfo({}) //get continents
      const states = await geonames.children({geonameId: countries.geonames[0].geonameId})
      const regions = await geonames.children({geonameId: states.geonames[0].geonameId});
      const cities = await geonames.children({geonameId: regions.geonames[0].geonameId});
      console.log(cities.geonames);
    }catch(err){
      console.error(err);
    }
    
    // promise
    geonames.countryInfo({}) 
    .then(countries => {
      return geonames.children({geonameId: countries.geonames[0].geonameId})
    })
    .then(states => {
      return geonames.children({geonameId: states.geonames[0].geonameId});
    })
    .then(regions => {
      return geonames.children({geonameId: regions.geonames[0].geonameId});
    })
    .then(cities => {
      console.log(cities.geonames);
    })
    .catch(err => console.log(err));

4. Contribution:

Feel free to contribute; any help is really appreciated :)

run with:

yarn build-dev (dev bundle)
yarn build (prod bundle)
yarn build:all (both - for packaging)
USERNAME=myusername yarn test (unit testing)

5. Changelog v3.0.3:

  • Added support for latest Geonames api (address, geoCodeAddress, streetNameLookup)
  • Added typescript autocomplete for all the api's
  • change function constructor to plain function
  • updated dependencies

img


6. License:

MIT 2017 License Karim Abdelcadir

geonames.js's People

Contributors

kinotto avatar xavitorello avatar lamuertepeluda avatar dependabot[bot] avatar piperchester avatar josephfrazier avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.