拖动后的 Google 地图标记位置
我可能在这里遗漏了 JavaScript 中的一些基本法则,很可能,但我有一个关于 Google 地图标记被拖动后的纬度位置的问题。
我成功地将标记添加到地图,并将这些添加的标记分配给变量,或者更准确地说,我使用 jQuery 将这些标记添加到地图 div 元素的 data
中,使用jQuery.data()
。
现在,当我在地图上拖动这些标记时,出现了问题。我了解 dragend
事件,以及如何触发/捕获它及其数据,包括拖动后的新 latlng 位置。话虽这么说,我理解 JavaScript 中的对象是通过引用传递的,所以我不明白的是为什么存储在元素中的标记引用的位置没有更新以自动反映新位置?
我猜这可能是以下原因之一:
- 传递到元素的
data
中的对象是重复的,而不是通过引用传递的,因此是一个 jQuery 问题。 - Google 地图不会自动更新存储在变量或数据节点中的标记的纬度位置,因此是 Google 地图的问题。
- 我必须亲自触发
dragend
事件来更新位置(听起来像是一个奇怪的解决方案)。
我基本上是在查询这个问题,看看我是否需要将其作为问题记录在任何地方,或者我是否只是一个白痴。来自其他成熟的谷歌地图绘制者的任何输入都会对我很有帮助。
提前干杯。
I may be missing some fundamental law in JavaScript here, quite possibly, but I have a question regarding the latlng position of a Google Maps marker after it is dragged.
I am successfully adding markers to a map, and assigning those added markers to a variable, or to be more precise, I'm using jQuery to add those markers to the data
of the map's div element, using jQuery.data()
.
Now, the problem I have arises when I drag those markers around the map. I understand the dragend
event, and how to fire/capture it and it's data, including the new latlng position after the drag. That being said, I was of the understanding that objects in JavaScript are passed by reference, so what I can't understand is why the position of the marker reference stored in the element isn't updated to reflect the new position automatically?
I guess it could be one of the following:
- Objects passed into the
data
of an element are duplicated, not passed by reference, and therefore a jQuery problem. - Google Maps doesn't automatically update the latlng positions of it's markers, stored in variables or
data
nodes, and therefore a Google Maps problem. - I have to physically fire the
dragend
event myself to update the position (sounds like an odd solution).
I'm basically querying this to see if I need to log it as an issue anywhere, or whether I'm just being an idiot. Any input from other hardened Google Mappers would really help me here.
Cheers in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到你已经解决了你的问题但是
也许这对其他人阅读这个问题有帮助。
Google 地图在拖尾后用新的 LatLng 对象覆盖标记的位置。如果将标记的位置(LatLng 对象)存储在变量中,则该变量将保存对先前定义的 LatLng 对象的引用。拖尾后,标记的位置会更新,但 LatLng 对象本身不会更新。
我们看一下下面的代码:
I've seen you have solved your problem but
maybe it's helpful for others reading this question.
Google Maps is overwriting the position of a Marker with a new LatLng-Object after dragend. If you store the position (LatLng-Object) of a marker in a variable, the variable holds a reference to the previously defined LatLng-Object. After the dragend the position of the marker gets updated but not the LatLng-Object itself.
Let's have a look at the following code: