有没有一种方法可以在react-leaflet中使用矢量切片?
我知道Leaflet.VectorGrid,但它并不是为react-leaflet编写的。
对于react-leaflet v2
,需要将MapBoxGLLayer
组件与HOC withLeaflet()
一起导出才能使其正常工作。
步骤:
1.安装mapbox-gl-leaflet
。
npm i mapbox-gl-leaflet
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' />
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);
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>
);
}
}
在这个react-leaflet issue中有一些非常不错的矢量切片示例(下面是重现的mapbox-gl示例)。
// @flow
import L from 'leaflet'
import {} from 'mapbox-gl-leaflet'
import {PropTypes} from 'react'
import { GridLayer } from 'react-leaflet'
export default class MapBoxGLLayer extends GridLayer {
static propTypes = {
opacity: PropTypes.number,
accessToken: PropTypes.string.isRequired,
style: PropTypes.string,
zIndex: PropTypes.number,
}
createLeafletElement(props: Object): Object {
return L.mapboxGL(props)
}
}
和上述组件的使用:
<Map>
<MapBoxGLLayer
url={url}
accessToken={MAPBOX_ACCESS_TOKEN}
style='https://style.example.com/style.json'
/>
</Map>
注意: 您可能还需要npm install mapbox-gl
并将该库导入并分配给全局变量window.mapboxgl = mapboxgl
,以避免出现mapboxgl
未定义的问题。
这是一个组件代码(TypeScript),用于MapLibre瓷砖图层,您可以在React Leaflet MapContainer
中使用它来替代TileLayer
:
import {
type LayerProps,
createElementObject,
createTileLayerComponent,
updateGridLayer,
withPane,
} from '@react-leaflet/core'
import L from 'leaflet'
import '@maplibre/maplibre-gl-leaflet'
export interface MapLibreTileLayerProps extends L.LeafletMaplibreGLOptions, LayerProps {
url: string,
attribution: string,
}
export const MapLibreTileLayer = createTileLayerComponent<
L.MaplibreGL,
MapLibreTileLayerProps
>(
function createTileLayer({ url, attribution, ...options }, context) {
const layer = L.maplibreGL({style: url, attribution: attribution, noWrap: true}, withPane(options, context))
return createElementObject(layer, context)
},
function updateTileLayer(layer, props, prevProps) {
updateGridLayer(layer, props, prevProps)
const { url, attribution } = props
if (url != null && url !== prevProps.url) {
layer.getMaplibreMap().setStyle(url)
}
if (attribution != null && attribution !== prevProps.attribution) {
layer.options.attribution = attribution
}
},
)
完整的示例代码存放在 GitHub 上的此仓库中:https://github.com/stadiamaps/react-leaflet-demo
L.mapboxGL()
函数由mapbox-gl-leaflet
提供。该库将此函数添加到全局变量L
中。您可以在这里查看:https://github.com/mapbox/mapbox-gl-leaflet/blob/b04674c701bada403ca428d08dce550b5ecd9863/leaflet-mapbox-gl.js#L236 - Murli Prajapati