使用React在双击事件上添加一个Mapbox标记

发布于 2025-01-25 20:02:52 字数 1140 浏览 3 评论 0原文

我有一些困难使我的代码工作。我正在使用交互式映射,并且想添加一个函数,当用户“双击”允许他们添加标记时,该功能应该从地图本身中拉出长度和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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖 2025-02-01 20:02:52

如果错误来自这一行:

const [longitude, latitude] = e.lngLat;

那么E.lnglat很有可能是您所期望的数组。该字段在哪里设置?它可能不会像您期望的那样设定。

If the error is coming from this line:

const [longitude, latitude] = e.lngLat;

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.

渡你暖光 2025-02-01 20:02:52

使用此代码

const { lat, lng: long } = e.lngLat;

const handleAddClick = (e) => {
  const [lat, long] = e.lngLat;
  setNewPlace({ lat, long });
};

use this code

const { lat, lng: long } = e.lngLat;

const handleAddClick = (e) => {
  const [lat, long] = e.lngLat;
  setNewPlace({ lat, long });
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文