谷歌地图-拖动标记,GetPosition总是返回相同的位置
Google Maps - Drag marker, GetPosition returning same position always
所以我有一个for循环,它创建了100多个标记。当我拖动一个随机标记时,我总是在控制台中得到相同的位置。但是当我拖动另一个标记时,"点"值不同。所以问题是为什么我总是处于同样的位置?如果我只尝试使用1个标记,则代码正在工作。
一段代码:
coordinates.forEach(function(entry) {
if(!isOdd(number)){
marker_lat = entry;
number++;
}else{
marker_lng = entry;
var myLatlng = new google.maps.LatLng(parseFloat(marker_lat),parseFloat(marker_lng));
marker_icon = "red.png";
mark = new google.maps.Marker({
position: myLatlng,
map: map,
title: number,
icon: marker_icon,
draggable:true
});
number++;
google.maps.event.addListener(mark, 'dragend', function() {
id_point = $(this).attr("title");
console.log("Point: "+id_point);
console.log(mark.getPosition().lat());// Always the same position
console.log(mark.getPosition().lng());// Always the same position
});
}
});
侦听器将标记作为参数传递给函数。试试这个代码:
google.maps.event.addListener(mark, 'dragend', function(m) {
id_point = $(this).attr("title");
console.log("Point: "+id_point);
console.log(m.latLng.lat());
console.log(m.latLng.lng()); /* different from your
code. Can't really
test right now, but
this seems to work on
the last project I
did. */
});
除此之外,您还可以在this
:上调用getPosition()
google.maps.event.addListener(mark, 'dragend', function(m) {
id_point = this.title;
console.log("Point: "+id_point);
console.log(this.getPosition().lat()); // same as below
console.log(m.latLng.lat());
});
相关文章:
- 如何将返回的值应用于多个不同位置的多个选择器
- 为更改的输入返回插入符号位置的逻辑
- float's未返回到移动视图结束后的位置
- Sammy.js缓存和滚动位置时,返回历史
- 谷歌位置api获取细节,并不是每个地方都返回
- 在从索引位置返回的字符串中查找空白
- 为什么 javascript 返回对象位置 true 或 false
- 地理位置无法返回确认
- 需要呈现MongoDB查询返回结果的特定索引/位置
- 计算地理位置距离返回 NaN
- 为什么&此脚本返回的位置未定义
- JSON获取地理位置在HTML中未返回值
- 如何解析html以删除元素并只返回其位置的标题
- 当向上滚动时,使Div返回到其原始位置
- 谷歌自动完成API首先返回我所在州的位置;预期的“;地方
- Javascript 函数返回位置 [0] 中的整个数组
- 打开/返回位置权限对话框
- 如果php返回位置头,我如何在jquery ajax中定位页面
- 领英Javascript SDK无法返回位置
- Deezer JS API返回位置:http://www.mydomain.com.br/dz/channel.html