The code in the sandbox actually works (for me anyway), but is misleading because the line drawn is nowhere near the viewport.

A couple of things to note are that coordinates are an array given in [long, lat] which may not be what most people would assume. For example, if you cut and paste [lat,long] from google maps for San Fransisco, you get [37.77909036739809, -122.41510269913951]. Then you'll have to reverse those and put them in:

const dataOne = {
    type: "Feature",
    properties: {},
    geometry: {
        type: "LineString",
        coordinates: [
            [-122.41510269913951, 37.77909036739809],
            [39.5423, -77.0564]
        ]
    }
};

Also, the sample code has some cruft in it. Edit the variable dataOne not the other unused place.

Now you'll see a line from San Fransisco to some random spot in the middle of Antarctica that was really easy to miss.

Just in case the link goes bad, the full code is:

import React, { Component } from "react";
import { render } from "react-dom";
import ReactMapGL, { Source, Layer } from "react-map-gl";
class App 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;
    const MAPBOX_TOKEN =
      "pk.eyJ1Ijoic21peWFrYXdhIiwiYSI6ImNqcGM0d3U4bTB6dWwzcW04ZHRsbHl0ZWoifQ.X9cvdajtPbs9JDMG-CMDsA";


    const dataOne = {
      type: "Feature",
      properties: {},
      geometry: {
        type: "LineString",
        coordinates: [
          [-122.41510269913951, 37.77909036739809],
          [39.5423, -77.0564]
        ]
      }
    };
    return (
      <ReactMapGL
        {...viewport}
        mapboxApiAccessToken={MAPBOX_TOKEN}
        onViewportChange={(newViewport) => {
          this.setState({ viewport: newViewport });
        }}
      >
        <Source id="polylineLayer" type="geojson" data={dataOne}>
          <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>
    );
  }
}

render(<App />, document.getElementById("root"));
Answer from Charlie G on Stack Overflow
🌐
npm
npmjs.com › package › react-map-gl-draw
react-map-gl-draw - npm
October 4, 2022 - A lite version editing layer with react. Latest version: 1.0.4, last published: 3 years ago. Start using react-map-gl-draw in your project by running `npm i react-map-gl-draw`. There are 18 other projects in the npm registry using react-map-gl-draw.
      » npm install react-map-gl-draw
    
Published   Oct 04, 2022
Version   1.0.4
Author   Xintong Xia
🌐
GitHub
github.com › urbica › react-map-gl-draw
GitHub - urbica/react-map-gl-draw: React Component for Mapbox GL Draw
React Component to enhance Urbica's React Map GL to use Mapbox Draw tools.
Starred by 53 users
Forked by 11 users
Languages   JavaScript 99.9% | Shell 0.1% | JavaScript 99.9% | Shell 0.1%
🌐
CodeSandbox
codesandbox.io › examples › package › react-map-gl-draw
react-map-gl-draw examples - CodeSandbox
Use this online react-map-gl-draw playground to view and fork react-map-gl-draw example apps and templates on CodeSandbox.
🌐
Urbica
urbica.github.io › react-map-gl-draw
Urbica React Map GL Draw
We cannot provide a description for this page right now
🌐
React Map GL
visgl.github.io › react-map-gl › examples
react-map-gl
Mapbox · Clusters · Markers, Popups and Controls · Custom Cursor · Draggable Marker · Draw Polygon · Highlight By Filter · Geocoder · GeoJSON Animation · GeoJSON · Heatmap · Limit Map Interaction · Dynamic Styling · Side by Side · Terrain · Camera Transition ·
Top answer
1 of 1
11

The code in the sandbox actually works (for me anyway), but is misleading because the line drawn is nowhere near the viewport.

A couple of things to note are that coordinates are an array given in [long, lat] which may not be what most people would assume. For example, if you cut and paste [lat,long] from google maps for San Fransisco, you get [37.77909036739809, -122.41510269913951]. Then you'll have to reverse those and put them in:

const dataOne = {
    type: "Feature",
    properties: {},
    geometry: {
        type: "LineString",
        coordinates: [
            [-122.41510269913951, 37.77909036739809],
            [39.5423, -77.0564]
        ]
    }
};

Also, the sample code has some cruft in it. Edit the variable dataOne not the other unused place.

Now you'll see a line from San Fransisco to some random spot in the middle of Antarctica that was really easy to miss.

Just in case the link goes bad, the full code is:

import React, { Component } from "react";
import { render } from "react-dom";
import ReactMapGL, { Source, Layer } from "react-map-gl";
class App 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;
    const MAPBOX_TOKEN =
      "pk.eyJ1Ijoic21peWFrYXdhIiwiYSI6ImNqcGM0d3U4bTB6dWwzcW04ZHRsbHl0ZWoifQ.X9cvdajtPbs9JDMG-CMDsA";


    const dataOne = {
      type: "Feature",
      properties: {},
      geometry: {
        type: "LineString",
        coordinates: [
          [-122.41510269913951, 37.77909036739809],
          [39.5423, -77.0564]
        ]
      }
    };
    return (
      <ReactMapGL
        {...viewport}
        mapboxApiAccessToken={MAPBOX_TOKEN}
        onViewportChange={(newViewport) => {
          this.setState({ viewport: newViewport });
        }}
      >
        <Source id="polylineLayer" type="geojson" data={dataOne}>
          <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>
    );
  }
}

render(<App />, document.getElementById("root"));
🌐
React Map GL
visgl.github.io › introduction
Introduction | react-map-gl
This does not scale when we have many components that need to synchronize with each other. We sometimes render two maps side by side, and when the user interacts with one, update both cameras. We draw React UI outside of the map container, that moves with the camera.
Find elsewhere
🌐
Snyk
snyk.io › advisor › react-map-gl-draw › react-map-gl-draw code examples
Top 5 react-map-gl-draw Code Examples | Snyk
/* global setTimeout */ import React, { PureComponent } from 'react'; import styled from 'styled-components'; import { EditorModes } from 'react-map-gl-draw'; const MODES = [ { id: EditorModes.SELECT, text: 'Edit Feature', icon: 'icon-select.svg' }, { id: EditorModes.DRAW_POINT, text: 'Draw Point', icon: 'icon-point.svg' }, { id: EditorModes.DRAW_PATH, text: 'Draw Polyline', icon: 'icon-path.svg' }, { id: EditorModes.DRAW_POLYGON, text: 'Draw Polygon', icon: 'icon-polygon.svg' }, { id: EditorModes.DRAW_RECTANGLE, text: 'Draw Rectangle', icon: 'icon-rectangle.svg' } ]; const Container = styled.div` position: absolute; width: 48px; left: 24px; top: 24px; background: #fff; box-shadow: 0 0 4px rgba(0, 0, 0, 0.15); outline: none; display: flex; justify-content: center; flex-direction: column; `;
🌐
Nebula
nebula.gl › docs › api-reference › react-map-gl-draw › react-map-gl-draw
React Map GL Draw
Advanced example: multiple draw modes and editing drawn features ... import React, { Component } from 'react'; import MapGL from 'react-map-gl'; import { Editor, EditingMode, DrawLineStringMode, DrawPolygonMode, } from 'react-map-gl-draw'; const MODES = [ { id: 'drawPolyline', text: 'Draw Polyline', handler: DrawLineStringMode }, { id: 'drawPolygon', text: 'Draw Polygon', handler: DrawPolygonMode }, { id: 'editing', text: 'Edit Feature', handler: EditingMode }, ]; const DEFAULT_VIEWPORT = { width: 800, height: 600, longitude: -122.45, latitude: 37.78, zoom: 14, }; class App extends Component { constructor(props) { super(props); this.state = { viewport: DEFAULT_VIEWPORT, modeId: null, modeHandler: null, }; } _switchMode = evt => { const modeId = evt.target.value === this.state.modeId ?
🌐
npm
npmjs.com › package › react-mapbox-gl-draw
react-mapbox-gl-draw - npm
If you wish to use Draw tools with react-mapbox-gl@2.x.x or react-mapbox-gl@v3.x.x, please use react-mapbox-gl-draw@1.0.6.
      » npm install react-mapbox-gl-draw
    
Published   Jan 03, 2020
Version   2.0.4
Author   Amaury Martiny
🌐
GitHub
github.com › amaury1093 › react-mapbox-gl-draw
GitHub - amaury1093/react-mapbox-gl-draw: Draw tools for Mapbox with React: 🗺️ react-mapbox-gl + 🖌️ mapbox-gl-draw
If you wish to use Draw tools with react-mapbox-gl@2.x.x or react-mapbox-gl@v3.x.x, please use react-mapbox-gl-draw@1.0.6.
Starred by 182 users
Forked by 28 users
Languages   TypeScript 63.7% | JavaScript 36.3% | TypeScript 63.7% | JavaScript 36.3%
🌐
Stack Overflow
stackoverflow.com › questions › 73552824 › mapbox-gl-draw-in-react-and-typescript
mapbox-gl-draw in react and typescript - Stack Overflow
In the latest version of react-map-gl they have added compatibility with the mapbox-gl-draw library: https://visgl.github.io/react-map-gl/docs/whats-new
🌐
React Map GL
visgl.github.io › what's new
What's new | react-map-gl
New Components: Layer and Source have been added to provide better React parity with the Mapbox GL JS API. Viewport transition: transitionDuration can be set to 'auto' when using FlyToInterpolator. New Example: Add an example with drawing library react-map-gl-draw.
🌐
Medium
medium.com › @jpan0831 › detect-marker-in-polygon-using-react-map-gl-draw-72a6c6f7f726
Detect Marker in Polygon using react-map-gl-draw | by Jay Pan | Medium
July 13, 2020 - Once the drawing is done, stop the drawing mode and, the polygon disappears and the markers which inside the polygon being highlighted · These three dependencies are the main key to this feature · npm i react-map-gl react-map-gl-draw point-in-polygon
🌐
CodeSandbox
codesandbox.io › examples › package › react-map-gl
react-map-gl examples - CodeSandbox
react-map-gl-draw-example · maplibre-basecontrolsdeckgl-mapboxoverlay-try · hex-bin-map · nebulagl-edit-mode · react-map-gl deck-gl template · dulldrums · react-map-gl with maplibre-gl · ddrempe · mapboxtesting · react-map-gl-geocoder-v2-example Example usage of react-map-gl-geocoder which is a React wrapper for mapbox-gl-geocoder for use with react-map-gl ·
🌐
React Map GL
visgl.github.io › adding custom data
Adding Custom Data | react-map-gl
import * as React from 'react'; import Map, {Source, Layer} from 'react-map-gl/maplibre'; import type {CircleLayer} from 'react-map-gl/maplibre'; import type {FeatureCollection} from 'geojson'; const geojson: FeatureCollection = { type: 'FeatureCollection', features: [ { type: 'Feature', geometry: { type: 'Point', coordinates: [-122.4, 37.8] }, properties: {title: '915 Front Street, San Francisco, California'} } ] }; const layerStyle: CircleLayer = { id: 'point', type: 'circle', paint: { 'circle-radius': 10, 'circle-color': '#007cbf' } }; function App() { return ( <Map initialViewState={{ longitude: -122.45, latitude: 37.78, zoom: 14 }}> <Source id="my-data" type="geojson" data={geojson}> <Layer {...layerStyle} /> </Source> </Map> ); }