使用React在双击事件上添加一个Mapbox标记
我有一些困难使我的代码工作。我正在使用交互式映射,并且想添加一个函数,当用户“双击”允许他们添加标记时,该功能应该从地图本身中拉出长度和lat。
我对使用该函数的部分有此内容:
<Map
initialViewState={{
center: [0,0],
zoom: 0.7,
}}
style={{width: "100vw", height: "100vh"}}
mapStyle="mapbox://styles/mapbox/streets-v11"
mapboxAccessToken={process.env.REACT_APP_TOKEN}
onDblClick = {handleAddClick}
>
这就是我的函数写的地方:
const handleAddClick = (e) =>{
const [longitude, latitude] = e.lngLat;
setNewIdea({
lat: latitude,
long: longitude,
});
};
这是我称之为函数的地方:
{newIdea && (
<Popup
longitude={newIdea.long}
latitude={newIdea.lat}
anchor="left"
closeButton ={true}
closeOnClick={false}
onClose={() => setNewIdea(null)}>
Hey
</Popup>
)}
</Map>
应用程序运行良好,但是当我双击时什么也不会发生。如果我在浏览器中打开控制台,每次双击时,我都会收到此错误:
Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
at handleAddClick (App.js:33:1)
不确定我在做什么错,任何帮助都将不胜感激!
I'm having some difficulties making my code work. I'm working on an interactive map and I want to add a function that when users "double click" it allows them to add a marker, the function is supposed to pull the long and lat from the map itself.
I have this for the part where the function is used:
<Map
initialViewState={{
center: [0,0],
zoom: 0.7,
}}
style={{width: "100vw", height: "100vh"}}
mapStyle="mapbox://styles/mapbox/streets-v11"
mapboxAccessToken={process.env.REACT_APP_TOKEN}
onDblClick = {handleAddClick}
>
And this is where my function is written:
const handleAddClick = (e) =>{
const [longitude, latitude] = e.lngLat;
setNewIdea({
lat: latitude,
long: longitude,
});
};
This is where I call the function:
{newIdea && (
<Popup
longitude={newIdea.long}
latitude={newIdea.lat}
anchor="left"
closeButton ={true}
closeOnClick={false}
onClose={() => setNewIdea(null)}>
Hey
</Popup>
)}
</Map>
The app runs well but nothing happens when I double click. If I open the console in the browser I get this error every time I double click:
Uncaught TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
at handleAddClick (App.js:33:1)
Not sure what I'm doing wrong, any help would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果错误来自这一行:
那么E.lnglat很有可能是您所期望的数组。该字段在哪里设置?它可能不会像您期望的那样设定。
If the error is coming from this line:
Then there is a good chance that e.lngLat is not an array as you’re expecting. Where is that field getting set? It’s likely not getting set in the way you’re expecting.
使用此代码
use this code