React-map-gl: how to add marker by click on map?

Created on 29 Jun 2019  路  2Comments  路  Source: visgl/react-map-gl

Hi
I want add marker by click, please help me

Most helpful comment

Use the onClick callback of <InteractiveMap />, grab lngLat from the PointerEvent

import React from 'react';
import InteractiveMap, {Marker} from 'react-map-gl'

function App(){
  const [markers, setMarkers] = React.useState([])
  const handleClick = ({lngLat: [longitude, latitude]}) => 
    setMarkers(markers => [...markers, {longitude, latitude}])

  return <InteractiveMap onClick={handleClick}>
   {markers.map((m, i) => <Marker {...m} key={i} />)}
  </InteractiveMap>
}

Demo

All 2 comments

Use the onClick callback of <InteractiveMap />, grab lngLat from the PointerEvent

import React from 'react';
import InteractiveMap, {Marker} from 'react-map-gl'

function App(){
  const [markers, setMarkers] = React.useState([])
  const handleClick = ({lngLat: [longitude, latitude]}) => 
    setMarkers(markers => [...markers, {longitude, latitude}])

  return <InteractiveMap onClick={handleClick}>
   {markers.map((m, i) => <Marker {...m} key={i} />)}
  </InteractiveMap>
}

Demo

You can also look at what https://nebula.gl/docs can do for you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cjmyles picture cjmyles  路  3Comments

miccferr picture miccferr  路  4Comments

sudoStatus200 picture sudoStatus200  路  5Comments

xoddong picture xoddong  路  4Comments

tbergman picture tbergman  路  3Comments