urbica / react-map-gl

React Component Library for Mapbox GL JS
https://urbica.github.io/react-map-gl/
MIT License
416 stars 50 forks source link

Search Place(GeoCoder) #296

Open manjunathrp opened 4 years ago

manjunathrp commented 4 years ago

Is your feature request related to a problem? Please describe. Need a search inside a map.. I found which can be outside a map

Describe the solution you'd like Search should be inside the map but there was nothing about geocoder/search

Describe alternatives you've considered I have used react-geocoder-autocomplete to search places now

Additional context Add any other context or screenshots about the feature request here. Screenshot from 2020-06-03 17-08-27

janbaykara commented 3 years ago
Captura de Tela 2021-01-19 às 17 43 46

Produced by:

// Usage

<MapGL>
   <GeocodeControl position='top-right' accessToken={mapboxApiAccessToken} />
</MapG>

// Component

import React, { useContext, useEffect } from 'react'
import MapGL, { MapContext } from '@urbica/react-map-gl'
import mapboxgl from 'mapbox-gl';
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';

function GeocodeControl ({ position, accessToken }: {
  accessToken: string,
  position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right',
}) {
  const map: mapboxgl.Map = useContext(MapContext)

  useEffect(() => {
    const control = new MapboxGeocoder({
      accessToken,
      mapboxgl
    })

    map?.addControl(
      control,
      position
    )

    return () => {
      map?.removeControl(control)
    }
  }, [map, position])

  return null
}

More reading