Skip to content

jackahl/react-geonames

Repository files navigation

react-geonames

Geocoder react component that is using Geonames API. Geonames is geographical
database with various free webservices, this component is using websevice for location
search. You can use it with Mapbox map service or another map service. React mapbox library
is used in example. See demo here.

NPM Code Style

Install

npm install --save react-geonames

Usage

import React, { Component } from 'react';

import Geocoder from 'react-geonames';
import ReactMapGL from 'react-map-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
// Importing additional styles
import 'react-geonames/dist/geonames.css';

import 'react-geonames/dist/index.css';

const queryParams = {
  type: 'json',
  maxRows: 10,
};

class App extends Component {
  state = {
    viewport: {
      width: 700,
      height: 500,
      latitude: 37.7577,
      longitude: -122.4376,
      zoom: 10,
    },
    placename: '',
  };

  onSelect = (place, placename) => {
    this.setState((prevState) => ({
      viewport: {
        ...prevState.viewport,
        latitude: +place.lat,
        longitude: +place.lng,
        zoom: 10,
      },
      placename: placename,
    }));
  };

  render() {
    return (
      <div>
        <Geocoder
          username=[YOUR_GEONAMES_USERNAME]
          https
          queryParams={queryParams}
          placeholder="Search"
          onSelect={this.onSelect}
        />
        <ReactMapGL
          {...this.state.viewport}
          onViewportChange={(nextViewport) => this.setState({ viewport: nextViewport })}
          mapboxApiAccessToken=[YOUR_MAPBOX_TOKEN]
          mapStyle="mapbox://styles/mapbox/streets-v11"
        />
      </div>
    );
  }
}

export default App;

Props

Property Type Description Default
onSelect function (required) Function that sets viewport or marker, receives
two arguments: selected location object
and formated location name
-
username string (required) Geonames username -
timeout number Debounce time between requests
while typing
300
https boolean Use HTTPS Geonames endpoint
(HTTP is used in their documentation
examples)
false
placeholder string Input field placeholder Search
queryParams object Geonames search parametars,
you can see documentation here
{
  type: 'json',
  maxRows: 10
}

Styling

Component has only basic style related to behavior of results,
you can style it yourself using these classes or import styling as shown
in example above.

Element Class
Geocoder container .react-geonames
Input element .react-geonames-input
Results list .react-geonames-results
Single result .react-geonames-item

License

MIT © nikolovskialeksandar

About

Geocoder react component that is using Geonames API.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • JavaScript 76.4%
  • HTML 19.1%
  • CSS 4.5%