svelte-mapbox
svelte-mapbox copied to clipboard
Access to Markers via code
I am currently adding markers to the map like this: (I have custom Marker Icons and popup code, this example is simplified).
<Map> {#if markers} {#each markers as m} <Marker lng={m.longitude} lat={m.latitude} label={m.name} /> {/each} {/if} </Map>
Is there a way to build these markers in code and add them to the map? I need the ability to add and remove markers based on the result of an API call.
Currently, when the API returns 0 markers I set the variable markers to null and receive this error: TypeError: Cannot read properties of null (reading 'removeChild')... I can add more detail if you need it.
Thank you.
You should probably set markers to an empty array and drop the if
- that might be the cause of the error.
you can interact with the map as you normally would using their JS api - so you can add markers dynamically in this way.