san*_*iya 1 leaflet reactjs react-leaflet
我已经使用 react-leaflet 创建了折线,我想使用 polylinedacorator 在折线上显示方向。但我不知道如何使用 react-leaflet 做到这一点。我找到了多个带有传单的示例,但没有使用 react-leaflet
const polyline = [[51.505, -0.09], [51.51, -0.1], [51.51, -0.12]]
export default class VectorLayersExample extends Component<{}> {
render() {
return (
<Map center={center} zoom={13}>
<TileLayer
attribution='&copy <a
href="http://osm.org/copyright">OpenStreetMap</a>
contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Polyline color="lime" positions={polyline} />
</Map>
)
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我如何使用带有上述代码的 polylinedacorators
Leaflet.PolylineDecorator可以集成React-Leaflet如下:
a) 安装leaflet和leaflet-polylinedecorator打包:npm i leaflet leaflet-polylinedecorator
b) 安装后,以下组件演示如何使用Polyline 组件L.polylineDecorator:
import React, { useRef, useEffect } from "react";
import { Map, TileLayer, Polyline, withLeaflet } from "react-leaflet";
import L from "leaflet";
import "leaflet-polylinedecorator";
const PolylineDecorator = withLeaflet(props => {
const polyRef = useRef();
useEffect(() => {
const polyline = polyRef.current.leafletElement; //get native Leaflet polyline
const { map } = polyRef.current.props.leaflet; //get native Leaflet map
L.polylineDecorator(polyline, {
patterns : props.patterns
}).addTo(map);
}, []);
return <Polyline ref={polyRef} {...props} />;
});
Run Code Online (Sandbox Code Playgroud)
用法
function MyMap(props) {
const { center, zoom } = props;
const polyline = [[57, -19], [60, -12]];
const arrow = [
{
offset: "100%",
repeat: 0,
symbol: L.Symbol.arrowHead({
pixelSize: 15,
polygon: false,
pathOptions: { stroke: true }
})
}
];
return (
<Map center={center} zoom={zoom}>
<TileLayer url="http://{s}.tile.osm.org/{z}/{x}/{y}.png" />
<PolylineDecorator patterns={arrow} positions={polyline} />
</Map>
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1130 次 |
| 最近记录: |