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 Overflownpm
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
GitHub
github.com › urbica › react-map-gl-draw
GitHub - urbica/react-map-gl-draw: React Component for Mapbox GL Draw
Starred by 53 users
Forked by 11 users
Languages JavaScript 99.9% | Shell 0.1% | JavaScript 99.9% | Shell 0.1%
Videos
19:11
react mapbox gl | Polygon layers tutorial - YouTube
50:04
Building with Mapbox GL JS in React - YouTube
react mapbox gl integration to show marker on map | React ...
03:00
React map with a marker maplibre gl js | 2023 tutorial step by ...
Chakra UI - MAP integration in React with react-map-gl and ...
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 ·
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.
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.
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 ·
Stack Overflow
stackoverflow.com › questions › tagged › react-map-gl
Newest 'react-map-gl' Questions - Stack Overflow
I'm using react-map-gl with clustering to display clustered features.
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> ); }