svelte-mapbox icon indicating copy to clipboard operation
svelte-mapbox copied to clipboard

Access to Markers via code

Open BrettBurbidge opened this issue 3 years ago • 1 comments

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.

BrettBurbidge avatar Jan 02 '22 01:01 BrettBurbidge

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.

antony avatar Jan 04 '22 16:01 antony