简体   繁体   中英

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.

没有标题的位置

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

带航向指示的位置

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

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM