有没有办法使用react-leaflet 中的矢量图块?
我知道Leaflet.VectorGrid,但它不是为 react-leaflet 编写的?
对于react-leaflet v2,导出MapBoxGLLayer用 HOC 包装的组件withLeaflet()以使其工作。
脚步:
1.安装mapbox-gl-leaflet。
npm i mapbox-gl-leaflet
Run Code Online (Sandbox Code Playgroud)
2.添加mapbox-gl js和css index.html
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.css' rel='stylesheet' />
Run Code Online (Sandbox Code Playgroud)
3.添加这个组件。
import L from "leaflet";
import {} from "mapbox-gl-leaflet";
import PropTypes from "prop-types";
import { GridLayer, withLeaflet } from "react-leaflet";
class MapBoxGLLayer extends GridLayer {
createLeafletElement(props) {
return L.mapboxGL(props);
}
}
/*
* Props are the options supported by Mapbox Map object
* Find options here:https://www.mapbox.com/mapbox-gl-js/api/#new-mapboxgl-map-options-
*/
MapBoxGLLayer.propTypes = {
accessToken: PropTypes.string.isRequired,
style: PropTypes.string
};
MapBoxGLLayer.defaultProps = {
style: "mapbox://styles/mapbox/streets-v9"
};
export default withLeaflet(MapBoxGLLayer);
Run Code Online (Sandbox Code Playgroud)
4.使用MapBoxGLLayer组件。
class App extends Component {
state = {
center: [51.505, -0.091],
zoom: 13
};
render() {
return (
<div>
<Map center={this.state.center} zoom={this.state.zoom}>
<MapBoxGLLayer
accessToken={MAPBOX_ACCESS_TOKEN}
style="mapbox://styles/mapbox/streets-v9"
/>
</Map>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
在此处找到工作代码(添加您自己的 mapbox 令牌):https ://codesandbox.io/s/ooypokn26y
| 归档时间: |
|
| 查看次数: |
4752 次 |
| 最近记录: |