Search code examples
javascriptreactjstypescriptreact-map-gl

react-map-gl showUserHeading not in GeolocateControl


I'm trying to display the users position and the heading indicator in React-mapbox-gl.

import * as React from "react";
import ReactMapGL, { GeolocateControl } from "react-map-gl";

const geolocateControlStyle = {
  right: 10,
  top: 10
};

function App() {
  const [viewport, setViewport] = React.useState({
    longitude: -122.45,
    latitude: 37.78,
    zoom: 14
  });
  return (
    <ReactMapGL
      {...viewport}
      width="80vw"
      height="80vh"
      onViewportChange={setViewport}
      mapboxApiAccessToken={
        "<token here>"
      }
    >
      <GeolocateControl
        style={geolocateControlStyle}
        positionOptions={{ enableHighAccuracy: true }}
        trackUserLocation={true}
        auto
      />
    </ReactMapGL>
  );
}

Currently I only have where the user is, not its current heading.

position without heading

What I want is to display the current heading, like this:,

position with heading indicatior

I've found the option for adding the heading indicator on the docs(https://docs.mapbox.com/mapbox-gl-js/api/markers/#geolocatecontrol), and there's an option for showUserHeading inm the react wrapper. And I can't wrap my head around how to add this option in the react wrapper?

Here's the GeoLocation props documentation: https://visgl.github.io/react-map-gl/docs/api-reference/geolocate-control


Solution

  • EDIT: v6.1.18 is released, and the GeolocateControl now has the showUserHeading prop. Therefore the solution below is no longer needed.

    I figured out that as of 21.09.2021, react-map-gl hasn't included showUserHeading within their props. I solved this by patching using this tool https://www.npmjs.com/package/patch-package

    Here's the patch

    diff --git a/node_modules/react-map-gl/src/components/geolocate-control.d.ts b/node_modules/react-map-gl/src/components/geolocate-control.d.ts
    index 7c5d2a4..949f38b 100644
    --- a/node_modules/react-map-gl/src/components/geolocate-control.d.ts
    +++ b/node_modules/react-map-gl/src/components/geolocate-control.d.ts
    @@ -9,6 +9,7 @@ type GeolocateControlProps = MapControlProps & Partial<{
       positionOptions: any,
       fitBoundsOptions: any,
       trackUserLocation: boolean,
    +  showUserHeading?: boolean,
       showUserLocation: boolean,
       showAccuracyCircle: boolean,
       onViewStateChange?: Function,