2.2.0 • Published 3 years ago

react-map-gl-geocoder v2.2.0

Weekly downloads
4,731
License
MIT
Repository
github
Last release
3 years ago

react-map-gl-geocoder

React wrapper for mapbox-gl-geocoder for use with react-map-gl.

NPM react-map-gl-geocoder

Demos

Installation

npm

$ npm install react-map-gl-geocoder

or

Yarn

$ yarn add react-map-gl-geocoder

Styling

Import:

import 'react-map-gl-geocoder/dist/mapbox-gl-geocoder.css'

or

Link tag in header:

<link href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.2.0/mapbox-gl-geocoder.css' rel='stylesheet' />

Props

Only mapRef and mapboxApiAccessToken are required.

All non-primitive prop values besides mapRef and containerRef should be memoized.

NameTypeDefaultDescription
mapRefObjectRef for react-map-gl map component.
containerRefObjectThis can be used to place the geocoder outside of the map. The position prop is ignored if this is passed in. Example: https://codesandbox.io/s/v0m14q5rly
onViewportChangeFunction() => {}Is passed updated viewport values after executing a query.
mapboxApiAccessTokenStringhttps://www.mapbox.com/
inputValueStringSets the search input value
originString"https://api.mapbox.com"Use to set a custom API origin.
zoomNumber16On geocoded result what zoom level should the map animate to when a bbox isn't found in the response. If a bbox is found the map will fit to the bbox.
placeholderString"Search"Override the default placeholder attribute value.
proximityObjectA proximity argument: this is a geographical point given as an object with latitude and longitude properties. Search results closer to this point will be given higher priority.
trackProximityBooleanfalseIf true, the geocoder proximity will automatically update based on the map view.
collapsedBooleanfalseIf true, the geocoder control will collapse until hovered or in focus.
clearAndBlurOnEscBooleanfalseIf true, the geocoder control will clear it's contents and blur when user presses the escape key.
clearOnBlurBooleanfalseIf true, the geocoder control will clear its value when the input blurs.
bboxArrayA bounding box argument: this is a bounding box given as an array in the format minX, minY, maxX, maxY. Search results will be limited to the bounding box.
typesStringA comma seperated list of types that filter results to match those specified. See https://www.mapbox.com/developers/api/geocoding/#filter-type for available types.
countriesStringA comma separated list of country codes to limit results to specified country or countries.
minLengthNumber2Minimum number of characters to enter before results are shown.
limitNumber5Maximum number of results to show.
languageStringSpecify the language to use for response text and query result weighting. Options are IETF language tags comprised of a mandatory ISO 639-1 language code and optionally one or more IETF subtags for country or script. More than one value can also be specified, separated by commas.
filterFunctionA function which accepts a Feature in the Carmen GeoJSON format to filter out results from the Geocoding API response before they are included in the suggestions list. Return true to keep the item, false otherwise.
localGeocoderFunctionA function accepting the query string which performs local geocoding to supplement results from the Mapbox Geocoding API. Expected to return an Array of GeoJSON Features in the Carmen GeoJSON format.
localGeocoderOnlyBooleanfalseIf true, indicates that the localGeocoder results should be the only ones returned to the user. If false, indicates that the localGeocoder results should be combined with those from the Mapbox API with the localGeocoder results ranked higher.
reverseGeocodeBooleanfalseEnable reverse geocoding. Defaults to false. Expects coordinates to be lat, lon.
enableEventLoggingBooleantrueAllow Mapbox to collect anonymous usage statistics from the plugin.
markerBoolean or ObjecttrueIf true, a Marker will be added to the map at the location of the user-selected result using a default set of Marker options. If the value is an object, the marker will be constructed using these options. If false, no marker will be added to the map.
renderFunctionA function that specifies how the results should be rendered in the dropdown menu. Accepts a single Carmen GeoJSON object as input and return a string. Any html in the returned string will be rendered. Uses mapbox-gl-geocoder's default rendering if no function provided.
positionString"top-right"Position on the map to which the geocoder control will be added. Valid values are "top-left", "top-right", "bottom-left", and "bottom-right".
onInitFunction() => {}Is passed Mapbox geocoder instance as param and is executed after Mapbox geocoder is initialized.
onClearFunction() => {}Executed when the input is cleared.
onLoadingFunction() => {}Is passed { query } as a param and is executed when the geocoder is looking up a query.
onResultsFunction() => {}Is passed { results } as a param and is executed when the geocoder returns a response.
onResultFunction() => {}Is passed { result } as a param and is executed when the geocoder input is set.
onErrorFunction() => {}Is passed { error } as a param and is executed when an error occurs with the geocoder.

Examples

Simple Example

import 'mapbox-gl/dist/mapbox-gl.css'
import 'react-map-gl-geocoder/dist/mapbox-gl-geocoder.css'
import React, { useState, useRef, useCallback } from 'react'
import MapGL from 'react-map-gl'
import Geocoder from 'react-map-gl-geocoder'

// Ways to set Mapbox token: https://uber.github.io/react-map-gl/#/Documentation/getting-started/about-mapbox-tokens
const MAPBOX_TOKEN = 'REPLACE_WITH_YOUR_MAPBOX_TOKEN'

const Example = () => {
  const [viewport, setViewport] = useState({
    latitude: 37.7577,
    longitude: -122.4376,
    zoom: 8
  });
  const mapRef = useRef();
  const handleViewportChange = useCallback(
    (newViewport) => setViewport(newViewport),
    []
  );

  // if you are happy with Geocoder default settings, you can just use handleViewportChange directly
  const handleGeocoderViewportChange = useCallback(
    (newViewport) => {
      const geocoderDefaultOverrides = { transitionDuration: 1000 };

      return handleViewportChange({
        ...newViewport,
        ...geocoderDefaultOverrides
      });
    },
    []
  );

  return (
    <div style={{ height: "100vh" }}>
      <MapGL
        ref={mapRef}
        {...viewport}
        width="100%"
        height="100%"
        onViewportChange={handleViewportChange}
        mapboxApiAccessToken={MAPBOX_TOKEN}
      >
        <Geocoder
          mapRef={mapRef}
          onViewportChange={handleGeocoderViewportChange}
          mapboxApiAccessToken={MAPBOX_TOKEN}
          position="top-left"
        />
      </MapGL>
    </div>
  );
};

export default Example

Ignore Map Events Example

You can use the containerRef prop to place the Geocoder component outside of the MapGL component to avoid propagating the mouse events to the MapGL component. You can use CSS to position it over the map as shown in this example.

import 'mapbox-gl/dist/mapbox-gl.css'
import 'react-map-gl-geocoder/dist/mapbox-gl-geocoder.css'
import React, { useState, useRef, useCallback } from 'react'
import MapGL from 'react-map-gl'
import Geocoder from 'react-map-gl-geocoder'

// Ways to set Mapbox token: https://uber.github.io/react-map-gl/#/Documentation/getting-started/about-mapbox-tokens
const MAPBOX_TOKEN = 'REPLACE_WITH_YOUR_MAPBOX_TOKEN'

const Example = () => {
  const [viewport, setViewport] = useState({
    latitude: 37.7577,
    longitude: -122.4376,
    zoom: 8,
  });
  const geocoderContainerRef = useRef();
  const mapRef = useRef();
  const handleViewportChange = useCallback(
    (newViewport) => setViewport(newViewport),
    []
  );

  return (
    <div style={{ height: "100vh" }}>
      <div
        ref={geocoderContainerRef}
        style={{ position: "absolute", top: 20, left: 20, zIndex: 1 }}
      />
      <MapGL
        ref={mapRef}
        {...viewport}
        width="100%"
        height="100%"
        onViewportChange={handleViewportChange}
        mapboxApiAccessToken={MAPBOX_TOKEN}
      >
        <Geocoder
          mapRef={mapRef}
          containerRef={geocoderContainerRef}
          onViewportChange={handleViewportChange}
          mapboxApiAccessToken={MAPBOX_TOKEN}
          position="top-left"
        />
      </MapGL>
    </div>
  );
};

Sample Screenshot

react-map-gl-geocoder example screenshot

2.2.0

3 years ago

2.1.7

3 years ago

2.1.6

4 years ago

2.1.4

4 years ago

2.1.5

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.16

4 years ago

2.0.15

4 years ago

2.0.14

4 years ago

2.0.13

4 years ago

2.0.12

4 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.4

5 years ago

1.6.3

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.8

5 years ago

1.5.7

5 years ago

1.5.6

5 years ago

1.5.5

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.3.2

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.1.1

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago