Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add a GeoJSON line like they do in the (mapbox-gl-js documentation) using react-map-gl?

I'm trying to add a GeoJSON line to show car direction between a point A and a point B ( like they have in the official documentation of mapbox-gl-js (https://www.mapbox.com/mapbox-gl-js/example/geojson-line/)

But the docs of react-map-gl doesn't talk about that at all.

How can I achieve something similar to this using https://uber.github.io/react-map-gl/#/Documentation/introduction/introduction :

here's my code so far :

class MapPage extends Component {
  constructor(props) {
    super(props);
     this.state = {
      viewport: {
       latitude: 38.63738602787579,
       longitude: -121.23576311149986,
       zoom: 6.8,
       bearing: 0,
       pitch: 0,
       dragPan: true,
       width: 600, 
       height: 600 
     }
    };
   }

render() {
  const { viewport } = this.state;

  return (
    <ReactMapGL
      {...viewport}
      mapboxApiAccessToken={MAPBOX_TOKEN}
      onViewportChange={newViewport => {
        this.setState({ viewport: newViewport });
      }}
    />
  );
 }
}

screen shot 2017-12-20 at 5 55 10 pm

like image 577
AziCode Avatar asked Dec 21 '17 02:12

AziCode


People also ask

How do I use GeoJSON in Mapbox?

You can upload GeoJSON files to Mapbox as tilesets using Mapbox Tiling Service or as datasets or tilesets using the Mapbox Uploads API. You can also upload GeoJSON files to Mapbox Studio, which uses the Uploads API, as either datasets or tilesets.

How do I draw a line in Mapbox?

The preferred way to draw lines within Mapbox GL JS is to express the lines as GeoJSON and add them to the map as a GeoJSONSource / line layer pair. You might find this example of drawing a GeoJSON line and this example of drawing a great arc line helpful.

How do I add a polyline in Mapbox?

The map provided by Mapbox doesn't have a Polyline component by default but we can leverage on other components that gives us the same result as what we get from Google Maps Polylines. To add a polyline on our map, we would be using the ShapeSource and LineLayer component provided by Mapbox.

How do you add a marker to Mapbox in GL JS?

Style markers First, add the CSS you'll need to style your markers. In this case, add the image file you downloaded as the background-image for a class called marker . In your same index. html file, copy and paste the code inside your style tag below the #map declaration.


2 Answers

Applicable for those who are using react-map-gl Version 5.0 onwards

As of October 2019, react-map-gl supports Layer and Source components, which is meant to allow developers to render Mapbox layers on the Mapbox canvas without the need to call getMap() to expose the underlying native Mapbox API.

You may refer to the original Mapbox Source and Layer documentations for the full specifications for the valid values for the Layer and Source props.

This is how the Source and Layer components can be used together with the code you have provided to generatea GeoJSON line on your map.

class MapPage extends Component {
  constructor(props) {
    super(props);
     this.state = {
      viewport: {
       latitude: 38.63738602787579,
       longitude: -121.23576311149986,
       zoom: 6.8,
       bearing: 0,
       pitch: 0,
       dragPan: true,
       width: 600, 
       height: 600 
     }
    };
   }

render() {
  const { viewport } = this.state;

  return (
    <ReactMapGL
      {...viewport}
      mapboxApiAccessToken={MAPBOX_TOKEN}
      onViewportChange={newViewport => {
        this.setState({ viewport: newViewport });
      }}
    >
      <Source id='polylineLayer' type='geojson' data={polylineGeoJSON}>
        <Layer
          id='lineLayer'
          type='line'
          source='my-data'
          layout={{
           'line-join': 'round',
           'line-cap': 'round',
          }}
          paint={{
           'line-color': 'rgba(3, 170, 238, 0.5)',
            'line-width': 5,
          }}
        />
      </Source>
    </ReactMapGL>
  );
 }
}
like image 142
wentjun Avatar answered Oct 28 '22 20:10

wentjun


You can get the mapbox-gl map object once the component mounts, then you can work with it directly. Try something like this:

class MapPage extends Component {
      constructor(props) {
        super(props);
         this.state = {
          viewport: {
           latitude: 38.63738602787579,
           longitude: -121.23576311149986,
           zoom: 6.8,
           bearing: 0,
           pitch: 0,
           dragPan: true,
           width: 600, 
           height: 600 
         }
        };
       }
    componentDidMount(){
      const map = this.reactMap.getMap();
      map.on('load', () => {
       //add the GeoJSON layer here
       map.addLayer({...})
      })
    }

    render() {
      const { viewport } = this.state;

      return (
        <ReactMapGL
          ref={(reactMap) => this.reactMap = reactMap} />
          {...viewport}
          mapboxApiAccessToken={MAPBOX_TOKEN}
          onViewportChange={newViewport => {
            this.setState({ viewport: newViewport });
          }}
        />
      );
     }
    }

React Refs: https://reactjs.org/docs/refs-and-the-dom.html

GetMap(): https://uber.github.io/react-map-gl/#/Documentation/api-reference/static-map?section=methods

like image 35
Kristofor Carle Avatar answered Oct 28 '22 21:10

Kristofor Carle