Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Place(GeoCoder) #296

Open
manjunathrp opened this issue Jun 3, 2020 · 1 comment
Open

Search Place(GeoCoder) #296

manjunathrp opened this issue Jun 3, 2020 · 1 comment

Comments

@manjunathrp
Copy link

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
Copy link

janbaykara commented Jan 19, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants