- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一个与 Leaflet 和 react-leaflet 一起工作的项目,我们还使用了一个名为 Leaflet.PolylineMeasure 的工具来进行测量距离。主要应用程序基于 babel 和老式的 React Classes 并且运行完美!最近,我们开始使用 TypeScript 和带有 hooks 的功能组件开发项目的新部分。在使用标尺功能时,我发现Leaflet.PolylineMeasure 在新环境中不起作用!根据添加所需库后插件的描述,新的 Measurer 函数 L.control.polylineMeasure(options)
应该会出现。但事实并非如此!在没有 TypeScript、hooks 的主应用程序中,使用相同包版本的 Leaflet 和 Leaflet.PolylineMeasure 一切仍然正常!我尝试将 map 组件重写为 React Class 组件,还尝试添加类型,但没有帮助。
知道问题的根源是什么吗?
最佳答案
我不确定错误是什么,因为您没有提供任何代码,但是这里是使用 react typescript,react-leaflet 的样子。您可以像这样扩展 MapControl 来实现 Leaflet.PolylineMeasure:
class PolylineMeasure extends MapControl {
createLeafletElement() {
return (L.control as any).polylineMeasure({
position: "topleft",
unit: "metres",
showBearings: true,
clearMeasurementsOnStop: false,
showClearControl: true,
showUnitControl: true
});
}
componentDidMount() {
const { map } = this.props.leaflet;
const polylineMeasure = this.leafletElement;
polylineMeasure.addTo(map);
}
}
然后在 Map 组件中使用它:
const Leaflet = () => {
return (
<Map
style={{ height: "100vh" }}
center={[48, 0]}
zoom={4}
minZoom={3}
maxZoom={18}
>
<TileLayer
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?"
/>
<PolylineMeasure />
</Map>
);
};
为了不出现 typescript 错误,您需要包含以下库:
有关更多详细信息,请查看此 demo
关于javascript - Leaflet.PolylineMeasure 在新环境中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59491060/
我们有一个与 Leaflet 和 react-leaflet 一起工作的项目,我们还使用了一个名为 Leaflet.PolylineMeasure 的工具来进行测量距离。主要应用程序基于 babel
我是一名优秀的程序员,十分优秀!