gpt4 book ai didi

javascript - React-leaflet-search 组件未渲染

转载 作者:行者123 更新时间:2023-12-02 22:54:00 28 4
gpt4 key购买 nike

当我尝试渲染react-leaflet-search时,出现错误。当我尝试使用该错误的解决方案时, react 正在编译,但组件似乎没有渲染。

我尝试了他们的示例 simple search 。遇到同样的错误

TypeError: Cannot read property 'map' of undefined
new ReactLeafletSearch
C:/Users/project/node_modules/react-leaflet-search/lib/React-Leaflet-Search.js:106
103 | });
104 | _this.SearchInfo = null; // searched lat,lng or response from provider
105 |
> 106 | _this.map = context.map || props.leaflet.map;
| ^ 107 | return _this;
108 | }
109 |

我尝试了这个解决方案(来自react-leaflet-search页面)

const searchComponent = props => (<ReactLeafletSearch position="topleft" />);

我什至尝试过这个:

const searchComponent = withLeaflet(ReactLeafletSearch);

这些行绕过了“错误”,但似乎没有组件正在渲染。

我的react-leaflet map 组件正在加载,缩放+/-按钮也在加载,但没有搜索。

这是我正在使用的代码 map example当然我也在做:

import { ReactLeafletSearch } from "react-leaflet-search";

唯一的区别是在第 31 行之后我添加了我的组件。

31. </Marker>
32. <searchComponent></searchComponent>
33. </Map>

似乎根本没有渲染任何东西。

我期望的结果是一个像这样的搜索组件 working example

最佳答案

重现this您需要的示例:

安装leafletreact-leafletreact-leaflet-search

之后,您需要安装 babel-polyfill ,如果您使用react-leaflet v2,则应该使用 withLeaflet 方法包装此组件。它还被记录为 github issue .

const ReactLeafletSearchComponent = withLeaflet(ReactLeafletSearch);

在示例的 SimpleExample 组件的 render 方法中:

export default class SimpleExample extends Component {
...

render() {
// here wrap it
const ReactLeafletSearchComponent = withLeaflet(ReactLeafletSearch);

return (
<Map
className="simpleMap"
scrollWheelZoom={true}
bounds={this.state.bounds}
maxZoom={this.state.maxZoom}
maxBounds={this.state.maxBounds}
>
<TileLayer
noWrap={true}
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<ReactLeafletSearchComponent
customProvider={this.provider}
position="topleft"
inputPlaceholder="Custom placeholder"
search={[33.100745405144245, 46.48315429687501]}
showMarker={true}
zoom={5}
showPopup={true}
popUp={this.customPopup}
closeResultsOnClick={true}
openSearchOnLoad={true}
// // these searchbounds would limit results to only Turkey.
searchBounds={[
[33.100745405144245, 46.48315429687501],
[44.55916341529184, 24.510498046875]
]}
// providerOptions={{region: 'tr'}}

// default provider OpenStreetMap
// provider="BingMap"
// providerKey="AhkdlcKxeOnNCJ1wRIPmrOXLxtEHDvuWUZhiT4GYfWgfxLthOYXs5lUMqWjQmc27"
/>
</Map>
);
}
}

不要忘记添加 map 高度并导入leaflet.css

Demo

关于javascript - React-leaflet-search 组件未渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58071816/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com