Placing markers by clicking
Click on the map to add markers
Click on the map to add markers:
Click the map to place markers, and drag them to move.
With useMarkers instead
The demo keeps its markers in component state. To keep them in a hook instead, and listen to clicks from a child component of the map, use useMarkers and useMapEvent:
import { SphereProvider, SphereMap, Marker, useMarkers, useMapEvent } from 'gistda-sphere-react';
function MarkerPlacer() {
const { items: markers, add, clear } = useMarkers();
useMapEvent('click', (location) => {
add({ position: location });
});
return (
<>
<button onClick={clear}>Clear</button>
{markers.map((m) => (
<Marker key={m.id} position={m.position} />
))}
</>
);
}
function App() {
return (
<SphereProvider apiKey="YOUR_API_KEY">
<SphereMap center={{ lon: 100.5, lat: 13.75 }} zoom={10} style={{ height: '500px' }}>
<MarkerPlacer />
</SphereMap>
</SphereProvider>
);
}