在react-mapbox-gl中从GeoJSON绘制多边形

时间:2016-08-29 21:40:21

标签: reactjs mapbox mapbox-gl mapbox-gl-js

我尝试使用uber team for mapbox gl编写的反应友好包装器。

我想知道是否有人使用他们的API成功渲染了geojson源的面要素。它声明<Layer/>组件上有一个源选项属性:

  

sourceOptions :选项对象合并到调用GeoJSONSource方法时使用的对象

按照geoJsonSource的mapbox API,我正在尝试以下内容,并想知道还需要做些什么才能让它呈现:

&#13;
&#13;
import React, { Component } from 'react';
import ReactMapboxGl, { Layer, Feature } from "../node_modules/react-mapbox-gl/dist";
import logo from './logo.svg';
import './App.css';

let containerStyle = {
    height: "100vh",
    width: "100vw"
};

const accessToken = _removed for safety_

class App extends Component {

    _polygonClicked = ({ feature }) => {
    console.log("Polygon clicked", feature.geometry.coordinates);
    };

    render() {
        return (
        <div className="App">
            <div className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <h2>Welcome to React</h2>
            </div>
            <ReactMapboxGl
                style={"mapbox://styles/mapbox/streets-v8"}
                center={[11.956511272000057,10.095463399000039]}
                zoom={[11]}
                accessToken={accessToken}
                containerStyle={containerStyle}>
                <Layer
                    type="fill"
                    paint={{ "fill-color": "#3bb2d0", "fill-opacity": .5 }}
                    id="testing"
                    sourceOptions={'religious',{
                        "type": 'geojson',
                        "data":'../small_poly/bridges.geojson'
                        }}
                    sourceId={'religious'}>
                </Layer>

            </ReactMapboxGl>
        </div>
    );
    }
}

export default App;
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

因此,我最终在onStyleLoad组件上使用了他们的<MapboxGl />属性,以便访问返回原始mapbox gl API的函数。解决方案远非完美,但它确实回答了我的基本问题。我猜它有点类似逃生舱的功能。

我按照他们的文档这一行:

  

要使用原始Mapbox API使用onStyleLoad属性,回调函数将接收map对象作为第一个参数,然后您可以使用mapbox gl API添加自己的逻辑。

代码看起来像这样:

class App extends Component {

    componentWillMount(){
        this.setState({
            center : [138.6000, -34.9286]
        })
    }

    _polygonClicked = ({ feature }) => {
    console.log("Polygon clicked", feature.geometry.coordinates);
    };

    _onStyleLoad = (map, event) => {
        console.log("map", map, "event: ", event, this.refs.map)
        map.addSource("16MAR13-FP-TOMNOD", {
            type: 'vector',
            tiles: ['https://s3.amazonaws.com/tomnod-vector-tiles/16MAR13-FP-TOMNOD/{z}/{x}/{y}']
        })
        map.addLayer({
            "id": "16MAR13-FP-TOMNOD",
            "type": "line",
            "source": "16MAR13-FP-TOMNOD",
            "source-layer": "16MAR13-FP-TOMNOD",
            "layout": {
                "visibility": "visible"
            },
            "paint": {},
            "interactive": true
     });
    }

    _onClick = () => {
        this.setState({
            center : [110,23]
        })
    }

    render() {
        return (
        <div className="App">
            <div className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
            </div>
            <ReactMapboxGl
                style={"mapbox://styles/mapbox/streets-v8"}
                center={this.state.center}
                zoom={[13]}
                accessToken={accessToken}
                containerStyle={containerStyle}
                onStyleLoad={this._onStyleLoad}
                onClick={this._onClick}
                ref='map'>
            </ReactMapboxGl>
        </div>
    );
    }
}

export default App
相关问题