gpt4 book ai didi

react-leaflet pointToLayer 更改图标的选项

转载 作者:行者123 更新时间:2023-12-04 12:48:15 25 4
gpt4 key购买 nike

我目前正在尝试学习 React,我想使用传单 map (react-leaflet)。

我有两个(点、多边形)GeoJson 对象要显示,它们正在工作,但我想替换默认标记图标。

传单文档 http://leafletjs.com/examples/geojson/告诉我使用 pointToLayer 选项。

代码:

onEachFeaturePoint、onEachfeaturePolygon、pointToLayer

onEachFeaturePoint(feature, layer) {
console.log('feature: ', feature);
console.log('layer: ', layer);
layer.on({
'click': function (e) {
console.log('e: ', e);
console.log('click');
}
})
}

onEachFeaturePolygon(feature, layer) {
console.log('feature: ', feature);
console.log('layer: ', layer);
layer.on({
'click': function (e) {
console.log('e: ', e);
console.log('click');
}
})
}

pointToLayer(feature, latlng) {
console.log('--- Point to layer');
console.log('feature: ', feature);
console.log('latlng: ', latlng);
return <CircleMarker center={latlng} />;
}

使成为
render() {
const center = [9.4921875, 51.83577752045248];

return (
<Map center={center} zoom={1}>
<GeoJSON ref='marker1' data={this.state.point} onEachFeature={this.onEachFeaturePoint.bind(this)} pointToLayer={this.pointToLayer.bind(this)} />
<GeoJSON ref='polygon1' data={this.state.polygon} onEachFeature={this.onEachFeaturePolygon.bind(this)} />
</Map>
)
}

如果我保留 pointToLayer={this.pointToLayer.bind(this)}它停止工作,出现以下错误:
Uncaught TypeError: layer.on is not a function
at CustomMarker.onEachFeaturePoint (MapDemo.js:73)
at NewClass.addData (leaflet-src.js:10455)
at NewClass.addData (leaflet-src.js:10435)
at NewClass.initialize (leaflet-src.js:10420)
at new NewClass (leaflet-src.js:310)
at L.geoJSON (leaflet-src.js:10732)

我无法弄清楚错误发生的原因,也许有人知道如何解决这个问题或帮助我理解我犯的错误。

编辑:通过替换 return <CirleMarker />return L.circleMarker()pointToLayer功能,我让它工作。

最佳答案

通过替换 return <CirleMarker />return L.circleMarker()pointToLayer功能,我让它工作。

import L from 'leaflet';

...

pointToLayer(feature, latlng) {
return L.circleMarker(latlng, null); // Change marker to circle
// return L.marker(latlng, { icon: {}}); // Change the icon to a custom icon
}

关于react-leaflet <GeoJSON/> pointToLayer 更改图标的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42437738/

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