如何使用react-map-gl在两点之间画线

Sea*_*Liu 8 mapbox-gl-js react-map-gl

我正在尝试使用react-map-gl 库在两点之间画一条线。我无法从官方文档中找到示例,因此我尝试从以下使用 Mapbox 库的代码片段中重现相同的行为

var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v11',
    center: [-122.486052, 37.830348],
    zoom: 15
});

map.on('load', function() {
    map.addSource('route', {
        'type': 'geojson',
        'data': {
            'type': 'Feature',
            'properties': {},
            'geometry': {
                'type': 'LineString',
                'coordinates': [
                    [-122.483696, 37.833818],

                    [-122.493782, 37.833683]
                ]
            }
        }
    });
    map.addLayer({
        'id': 'route',
        'type': 'line',
        'source': 'route',
        'layout': {
            'line-join': 'round',
            'line-cap': 'round'
        },
        'paint': {
            'line-color': '#888',
            'line-width': 8
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

这是沙箱,我在控制台上没有看到任何错误,但未显示该行:
https://codesandbox.io/s/draw-line- Between-two-point-v0mbc?file=/src/index。 js:214-226

Cha*_*e G 8

沙箱中的代码实际上可以工作(无论如何对我来说),但具有误导性,因为绘制的线远离视口。

需要注意的一些事情是,坐标是一个以 [long, lat] 给出的数组,这可能不是大多数人所假设的。例如,如果您从旧金山的谷歌地图中剪切并粘贴 [lat,long],您将得到 [37.77909036739809, -122.41510269913951]。然后你必须反转它们并将它们放入:

const dataOne = {
    type: "Feature",
    properties: {},
    geometry: {
        type: "LineString",
        coordinates: [
            [-122.41510269913951, 37.77909036739809],
            [39.5423, -77.0564]
        ]
    }
};
Run Code Online (Sandbox Code Playgroud)

此外,示例代码也有一些缺陷。编辑变量dataOne而不是其他未使用的地方。

现在你会看到一条从旧金山到南极洲中部某个随机地点的线路,很容易错过。

以防万一链接出错,完整代码是:

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"));
Run Code Online (Sandbox Code Playgroud)

  • 花了几个小时后,我确实意识到我弄乱了经度和纬度 (2认同)