【问题标题】:How to update and get Circle new position after dragging using @react-google-maps/api?使用@react-google-maps/api 拖动后如何更新和获取 Circle 新位置?
【发布时间】:2021-10-20 16:34:39
【问题描述】:

我在地图上画了一个圆圈,我想知道如何更新并在拖动圆圈后获取新的经纬度位置,这是我目前的代码:

<GoogleMap
  onLoad={loadHandler}
  center={center}
  zoom={zoom}
  mapContainerStyle={{
    height: "70vh",
    width: "100%"
  }}
>
  <Circle
    ref={circleRef}
    center={center}
    radius={1000}
    options={{ fillColor: "#EE80AE33", strokeColor: "#EE80AE66" }}
    draggable={true}
    // onDragStart={}
    onDragEnd={() => console.log(circleRef.current.getCenter())}
  />
</GoogleMap>

我尝试了onDragEnd={() =&gt; console.log(circleRef.current.getCenter())},但它不起作用,因为我得到了原始中心位置

查看文档后here

以及代码示例here

【问题讨论】:

    标签: javascript reactjs google-maps react-google-maps


    【解决方案1】:

    我认为中心没有更新,因为center 是在 state 中定义的;该值将始终固定,因为它从未手动更改。拖动时可能会绘制圆形,但不会在内部更新中心。

    onDragEnd 传递一个地图事件,您可以从中解构 latLng 对象,其中包含一些应该返回新中心的有用函数。

    onDragEnd={(event) => console.log(event.latLng.toString())}
    

    【讨论】:

    • 这太棒了!但它没有给出圆的中心坐标,它给出了鼠标指针位置的坐标,所以我想我需要计算一个偏移量并进行相应的调整
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多