【问题标题】:How to use react-map-gl to draw line between two point如何使用 react-map-gl 在两点之间画线
【发布时间】:2021-06-04 18:28:05
【问题描述】:

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

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
        }
    });
});

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

【问题讨论】:

    标签: mapbox-gl-js react-map-gl


    【解决方案1】:

    沙盒中的代码确实有效(无论如何对我来说),但由于绘制的线不在视口附近,所以会产生误导。

    需要注意的一点是,坐标是 [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]
            ]
        }
    };
    

    此外,示例代码中有一些内容。编辑变量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"));
    

    【讨论】:

    • 花了几个小时后,我确实意识到我搞砸了经度和纬度
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 2017-09-24
    • 1970-01-01
    相关资源
    最近更新 更多