react leaflet updating markers

偶尔善良 提交于 2020-06-01 05:54:13

问题


hello I am a leaflet newbie, I am building my app with map component. the marker on the map should be updated every time the user perform specific action (filters) . the component was working fine . then I decided to add the responsive popup plugin from the original Leaflet. so I tweaked a lot the normal component of react leaflet . now every time I perform I new filters the new markers are added successfully to the map however the old marker are not deleted.

codesandbox demo

any help on how to fix this ?


回答1:


If you are not bound to that component (leaflet-responsive-popup) you can solve it like this (preferred solution): CodeSandbox without ResponsivePopup

<Marker key={i} position={bus}>
  <Popup>
    <span>Note<br />{i}</span>
  </Popup>
</Marker>

If you have to use that component than you have to store markers in an array and clear all the markers before you add new ones like this: CodeSandbox with ResponsivePopup Problem is that you were adding markers, while never removing the old ones. You have to pass all the coordinates so that first you could remove the old ones and than add the new ones.



来源:https://stackoverflow.com/questions/61873505/react-leaflet-updating-markers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!