admin管理员组文章数量:1426014
I'm trying to create a custom ponent in react-leaflet v2
extending a leaflet plugin EdgeMarker. The documentation does not really give details on how to do this. So I have copied the Leaflet.EdgeMarker.js
file from the repo and added it to my implementation.
This is what I have done so far:
import PropTypes from 'prop-types';
import { MapLayer, withLeaflet } from 'react-leaflet';
import L from 'leaflet';
import '../EdgeMarker/EdgeMarker';
class EdgeMarkerComp extends MapLayer{
static childContextTypes = {
layerContainer: PropTypes.object
}
getChildContext () {
return {
layerContainer: this.leafletElement
}
}
createLeafletElement(props) {
const { options } = props;
console.log("Options: ", options);
return new L.EdgeMarker(options);
}
}
export default withLeaflet(EdgeMarkerComp);
On my map:
const options = {
icon: L.icon({ // style markers
iconUrl: 'images/edge-arrow-marker-black.png',
clickable: true,
iconSize: [48, 48],
iconAnchor: [24, 24]
}),
rotateIcons: true,
layerGroup: null
};
<Map ...>
<EdgeMarkerComp options={options} />
</Map>
Any help???
I'm trying to create a custom ponent in react-leaflet v2
extending a leaflet plugin EdgeMarker. The documentation does not really give details on how to do this. So I have copied the Leaflet.EdgeMarker.js
file from the repo and added it to my implementation.
This is what I have done so far:
import PropTypes from 'prop-types';
import { MapLayer, withLeaflet } from 'react-leaflet';
import L from 'leaflet';
import '../EdgeMarker/EdgeMarker';
class EdgeMarkerComp extends MapLayer{
static childContextTypes = {
layerContainer: PropTypes.object
}
getChildContext () {
return {
layerContainer: this.leafletElement
}
}
createLeafletElement(props) {
const { options } = props;
console.log("Options: ", options);
return new L.EdgeMarker(options);
}
}
export default withLeaflet(EdgeMarkerComp);
On my map:
const options = {
icon: L.icon({ // style markers
iconUrl: 'images/edge-arrow-marker-black.png',
clickable: true,
iconSize: [48, 48],
iconAnchor: [24, 24]
}),
rotateIcons: true,
layerGroup: null
};
<Map ...>
<EdgeMarkerComp options={options} />
</Map>
Any help???
Share Improve this question asked Jul 3, 2018 at 20:47 danyhioldanyhiol 6792 gold badges8 silver badges26 bronze badges2 Answers
Reset to default 4I finally solved the problem by using the map directly. This was even more convenient and gave me more freedom while adding interaction to the map. Simply define your map as following and use the map reference to perform any action:
<Map
ref={Map => this.map = Map}
...
>
...
<Map>
Now you can reference the map in any leaflet object using this.map.leafletElement
defined by react-leaflet:
const polyline = L.polyline([p1,p2 ], {color: 'yellow'}).addTo(this.map.leafletElement);
The above code will add a new line on the map.
polyline.remove();
=> will remove the line from the map.
Yeah this used to be very straightforward with v1.x of react-leaflet
I've run into a similar extension problem and I've opened an issue on the react-leaflet github repo in the hopes that Paul will show us the way. https://github./PaulLeCam/react-leaflet/issues/506
For now downgrading to v1.9 works for me.
本文标签: javascriptAdd leaflet plugins to ReactLeafletStack Overflow
版权声明:本文标题:javascript - Add leaflet plugins to React-Leaflet - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745389228a2656518.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论