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

Geocoder doesn't appear in Next.js app #103

Open
bbaervoets opened this issue Apr 5, 2021 · 1 comment
Open

Geocoder doesn't appear in Next.js app #103

bbaervoets opened this issue Apr 5, 2021 · 1 comment

Comments

@bbaervoets
Copy link

Hi,

I'm trying to use react-map-gl-geocoder in my Next.js application.
The mapbox-gl-geocoder appears on my screen, but I can't see the geocoder.
Nonetheless, the geocoder appears in my react dev tools.
I tried out many things before it occured to me that Next.js is probably causing this problem.
The code below is from one of the examples.
Does anyone have experience with react-map-gl-geocoder + Next.js ?

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'

const MAPBOX_TOKEN = 'private'

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

  const handleGeocoderViewportChange = useCallback(
    (newViewport) => {
      const geocoderDefaultOverrides = { transitionDuration: 1000 }

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

  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={handleGeocoderViewportChange}
          mapboxApiAccessToken={MAPBOX_TOKEN}
          position="top-left"
        />
      </MapGL>
    </div>
  )
}

export default Location;

Screenshot 2021-04-05 at 23 17 34

@SamSamskies
Copy link
Owner

SamSamskies commented Apr 5, 2021

Hi @bbaervoets, there is an issue with @mapbox/mapbox-gl-geocoder that is causing this. I believe for now you can get around the issue by using Next dynamic imports https://nextjs.org/docs/advanced-features/dynamic-import. See this issue for more details #36. I have an open PR with @mapbox/mapbox-gl-geocoder to fix the issue. Feel free to give the PR a +1 mapbox/mapbox-gl-geocoder#403 😀.

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