Adding markers from state
Add and remove markers with React state or the useMarkers hook
Add and remove markers from code. The demo keeps them in useMarkers, which gives each marker an ID for its key.
With React state instead
Plain React state works for any overlay too. Give each one a stable key:
import { useState } from 'react';
import { SphereProvider, SphereMap, Marker, type Location } from 'gistda-sphere-react';
function App() {
const [markers, setMarkers] = useState<Location[]>([]);
const addMarker = () => {
const newMarker = {
lon: 100.5 + (Math.random() - 0.5) * 0.2,
lat: 13.75 + (Math.random() - 0.5) * 0.2,
};
setMarkers([...markers, newMarker]);
};
return (
<SphereProvider apiKey="YOUR_API_KEY">
<button onClick={addMarker}>Add Marker</button>
<button onClick={() => setMarkers([])}>Clear All</button>
<SphereMap center={{ lon: 100.5, lat: 13.75 }} zoom={11} style={{ height: '500px' }}>
{markers.map((position) => (
<Marker key={`${position.lon}-${position.lat}`} position={position} />
))}
</SphereMap>
</SphereProvider>
);
}