Giter Club home page Giter Club logo

Comments (4)

icebox avatar icebox commented on July 22, 2024 1

I was having the same issue but I am not using TypeScript.

It appears react-map-gl-geocoder v2.1.6 is not compatible with the latest versions of react-map-gl v6.1.x

react-map-gl v6.1.0-alpha introduces a breaking change somewhere.

Switching to react-map-gl v6.0.3 works for me. I am also using the latest version of react.

react-map-gl-geocoder

from react-map-gl-geocoder.

uebriges avatar uebriges commented on July 22, 2024

Hi,

I am using Typescript and tried the solution from @icebox, but still get this error message: TypeError: Cannot read property 'addControl' of null.

I have the same versions as mentioned by @icebox

My code looks like this:

/** @jsxImportSource @emotion/react */
import 'next';
import Head from 'next/head';
import { useCallback, useRef, useState } from 'react';
import ReactMapGL from 'react-map-gl';
import Geocoder from 'react-map-gl-geocoder';

// Create the parameters for the routing request:

// const DynamicComponentWithNoSSR = dynamic(() => import('../components/Map'), {
//   ssr: false,
// });

// const dynamicComponent = () => <DynamicComponentWithNoSSR />;
// export default dynamicComponent;

type ViewportType = {
  width: string;
  height: string;
  latitude: number;
  longitude: number;
  zoom: number;
};

type HomeType = {
  mapboxToken: string;
};

export default function Home(props: HomeType) {
  const [viewport, setViewport] = useState({
    width: '100vw',
    height: '100vh',
    latitude: 48.8685,
    longitude: 2.328549,
    zoom: 15,
  });
  const mapRef = useRef();
  const handleViewportChange = useCallback(
    (newViewport) => setViewport(newViewport),
    [],
  );

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

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

  return (
    <div>
      <Head>
        <title>Create Next App</title>
        <link rel="icon" href="/favicon.ico" />
        <link
          href="https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css"
          rel="stylesheet"
        />
      </Head>

      <main>
        <ReactMapGL
          {...viewport}
          mapStyle="mapbox://styles/mapbox/streets-v9"
          mapboxApiAccessToken={props.mapboxToken}
          onViewportChange={handleViewportChange}
        >
          <Geocoder
            mapRef={mapRef}
            onViewportChange={handleGeocoderViewportChange}
            mapboxApiAccessToken={props.mapboxToken}
            position="top-left"
          />
        </ReactMapGL>
        {/* <Map viewport={viewport} setViewport={setViewport} /> */}
      </main>
    </div>
  );
}

export function getServerSideProps() {
  return { props: { mapboxToken: process.env.MAPBOX_API_TOKEN || null } };
}

Anyone having a solution for that?

Thanks a lot.

from react-map-gl-geocoder.

SamSamskies avatar SamSamskies commented on July 22, 2024

Hey @uebriges, you need to pass mapRef as a ref to ReactMapGL. The geocoder uses that handle to access the Mapbox map instance from the React Map GL instance.

from react-map-gl-geocoder.

uebriges avatar uebriges commented on July 22, 2024

Hi @SamSamskies,

Thanks a lot. It worked now also with the latest version of react-map-gl (6.1.10).

from react-map-gl-geocoder.

Related Issues (20)

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.