Code Monkey home page Code Monkey logo

react-mapbox-gl-geocoder's Introduction

react-mapbox-gl-geocoder

Simple react autocomplete for geocoding locations using Mapbox API. You can use it with react-map-gl to pan to found locations.

NPM JavaScript Style Guide

Install

npm install --save react-mapbox-gl-geocoder
# or
yarn add react-mapbox-gl-geocoder

Usage

import React, {Component} from 'react'
import Geocoder from 'react-mapbox-gl-geocoder'
import ReactMapGL from 'react-map-gl'
import './index.css'

const mapAccess = {
    mapboxApiAccessToken: process.env.REACT_APP_MAPBOX_API_ACCESS_TOKEN
}

const mapStyle = {
    width: '100%',
    height: 600
}

const queryParams = {
    country: 'us'
}

class App extends Component {
    state = {
        viewport: {}
    }

    onSelected = (viewport, item) => {
        this.setState({viewport});
        console.log('Selected: ', item)
    }

    render() {
        const {viewport} = this.state

        return (
            <div>
                <Geocoder
                    {...mapAccess} onSelected={this.onSelected} viewport={viewport} hideOnSelect={true}
                    queryParams={queryParams}
                />

                <ReactMapGL
                    {...mapAccess} {...viewport} {...mapStyle}
                    onViewportChange={(newViewport) => this.setState({viewport: newViewport})}
                />
            </div>
        )
    }
}

Props

Name Type Description Default
mapboxApiAccessToken (required) string Mapbox Access Token -
timeout int Debounce between pressing the key and quering the results 300
viewport object Map Viewport -
onSelected (required) function(viewport, item) Receives selected item and the viewport to set for the selected item -
transitionDuration int Duration of the smooth transition 0
hideOnSelect bool Whether to hide results on select or not false
updateInputOnSelect bool Whether to set the input value to the selected location false
pointZoom int Zoom to set if a specific location (without bbox) is selected 16
formatItem function(item) Function used for formatting results item => item.place_name
className string Class to add to the top component -
queryParams object Query parameters to use when searching of the results, you can see available options here -
limit int Limit of the results 5
localGeocoder function(queryString) Function to supplement local results to geocoder -
initialInputValue string The initial input value ''

Styling

This component does NOT come with any styles. You can style it yourself by using classes:

Class name Description
.react-geocoder component wrapper class
.react-geocoder-results results wrapper class
.react-geocoder-item single result class

Replacing components

You can even supplement your own components:

Property name Passed Properties Properties description
inputComponent onChange Function to call when the text changes
itemComponent item, onClick, children children is item formatted to display

License

MIT © groinder

react-mapbox-gl-geocoder's People

Contributors

groinder avatar jessicaonly avatar tobeycodes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-mapbox-gl-geocoder's Issues

Backslash Error

image

I stumbled across an error which happens when the user enters in a '\' character. I would imagine that other URI special characters also can cause some issues in the same manner.

  • NOTE: I am using a custom input component and have not tried with the default input.

Usage with Deck.gl

Is there a way to use this package with Deck.gl as parent of a StaticMap component?

Styling not working

Trying to style the the component the same way as on example but it's not working

queryParams not taken in account

Hello and thank for this nice library!
I'm passing a queryParams prop to geocoder but it doesn't change the request. Am I doing it wrong?

const queryParams = {
  bbox: [  bbox.sw.lng, bbox.sw.lat,  bbox.ne.lng,  bbox.sw.lng  ],
  proximity: [center.longitude, center.latitude],
};

Result in network console stays the same and does not include my params:

https://api.mapbox.com/geocoding/v5/mapbox.places/me.json?limit=5&language=fr-FR&access_token=[secret]

Support React 17

React 17 is out now, but this lib has a peer-dependency pinned to 16.4.1.
This new react version will allow more flexibility when working with different versions of react, so we don't need to keep bumping the version in the future.

Clear address text result in Geocoder Search field

Hi.

We are very satisfied with the Geocoder Component in most respects.

We have put the Geocoder component outside the react-mapbox-gl component and use it to find adress results resulting from the user choosing one of the address alternatives appearing beneath the address field.

BUT: We are unable to clear the geocoder address field.
We typically use the geocoder in the following manner:

CREATE A NEW ORDER IN AN ORDER FORM:

  • Find an address
  • Fill in other related info
  • Store the result (for exampel an order of some kind)
  • Give a response to the user that the order has been stored including an order reference.
  • When we give the response, we want to clear all the fields in the order form.

We are able to clear all fields, except the geocoder address fields (which are controlled by state variables).
We prefer to use react's rendering functionality (which is ajax like)
We do not want to simulate a complete page render (F5 simulation).

Any clues regarding how we can clear the geocoder address fields would be greatly appreciated.

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.