简体   繁体   English

更改打字稿传单中 L.Symbol.arrowhead 的颜色

[英]Change the color of the L.Symbol.arrowhead in typescript Leaflet

I am implementing L.symbol.arrowhead to show arrows on polyline by using polyline-decorator plugin.我正在实现 L.symbol.arrowhead 通过使用 polyline-decorator 插件在折线上显示箭头。 I am able to dynamically change the polylines color, however, I am not able to dynamically change the color of the arrows.我能够动态更改折线颜色,但是,我无法动态更改箭头的颜色。 I am using react-leaflet with typscript.我正在使用带有打字稿的反应传单。

function PolylineDecorator({ patterns, polyline,color }) {
  const map = useMap();
  useEffect(() => {
    if (!map) return;
   L.polyline(polyline, {color}).addTo(map);  // added color property
    L.polylineDecorator(polyline, {
      patterns,
    }).addTo(map);
  }, [map]);
 
  return null;
}

const arrow = [
{
  offset: "100%",
  repeat: 0,
  symbol: L.Symbol.arrowHead({
    pixelSize: 15,
    polygon: false,
    pathOptions: { stroke: true }
  })
}];


 {currentData?.movingActors.map(line =>(<PolylineDecorator key={line.id} patterns ={arrow} polyline={position}  color = {modeColor(line.mode)} />) ) }   //here I used color parameters to dynamically add colors

When I try to access the pathOptions via当我尝试通过

arrow.forEach((line) => {line.symbol.pathOptions})

the typescript error I get is我得到的打字稿错误是

"Property 'pathOptions' does not exist on type 'ArrowHead" “类型‘ArrowHead’上不存在属性‘pathOptions’”

I have solved my problem by using Arrowhead plugin.我已经通过使用箭头插件解决了我的问题。 It is much easier as it takes the same color as that of the polyline.它更容易,因为它采用与折线相同的颜色。

So my new solution looks like所以我的新解决方案看起来像

function PolylineDecorator({polyline,color }) {
  const map = useMap();
  useEffect(() => {
    if (!map) return;
  var x = L.polyline(polyline, {color}).arrowheads({ size: '5%' });
  x.addTo(map);
  }, [map]);
 
  return null;
}


 {currentData?.movingActors.map(line =>(<PolylineDecorator key={line.id} polyline={position}  color = {modeColor(line.mode)} />) ) }  

However, if someone else has made it work with L.symbol.arrowhead then please update it here for others.但是,如果其他人已使其与 L.symbol.arrowhead 一起使用,请在此处对其进行更新以供其他人使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM