Google 地图标记 marker.setMap(null) 不是函数
Google Maps Markers marker.setMap(null) not a function
我知道这个问题似乎与很多其他问题相似。
以下问题。我用数组保存 Google 个地图标记。
如果我想更新它们,我无法编写 marker.set(Something)。我必须写 marker.j.map = null;
- 我是不是漏掉了什么?
例如
var markers = [];
function doAddMarkers(){
markers.push(new google.maps.Marker({
[...]
}).addListener('click', (function (i) {
[...]
}).bind(this, "xxx"), false));
}
function DoStartHl(pElement) {
for (var i = 0; i < markers.length; i++) {
if (pElement !== i && markers[i].j.animation !== null) {
markers[i].j.animation = null;
}
}
if (markers[parseInt(pElement.id)].j.animation === null) {
markers[parseInt(pElement.id)].j.animation = google.maps.Animation.BOUNCE;
}
}
更新
解决方法是在当前标记和setter之间添加一个.j.
。
您正在使用索引遍历标记数组。因此,未定义标记。您应该通过标记数组中的索引引用标记
function DoClearMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].j.map = null;
markers[i].map = null;
// marker.setMap(null); <= Won't work. ".setMap() is not a function"
//the above won't work because `marker` is not defined.
//instead, reference the marker by its index in the array:
markers[i].setMap(null);
}
markers = [];
}
我知道这个问题似乎与很多其他问题相似。
以下问题。我用数组保存 Google 个地图标记。
如果我想更新它们,我无法编写 marker.set(Something)。我必须写 marker.j.map = null;
- 我是不是漏掉了什么?
例如
var markers = [];
function doAddMarkers(){
markers.push(new google.maps.Marker({
[...]
}).addListener('click', (function (i) {
[...]
}).bind(this, "xxx"), false));
}
function DoStartHl(pElement) {
for (var i = 0; i < markers.length; i++) {
if (pElement !== i && markers[i].j.animation !== null) {
markers[i].j.animation = null;
}
}
if (markers[parseInt(pElement.id)].j.animation === null) {
markers[parseInt(pElement.id)].j.animation = google.maps.Animation.BOUNCE;
}
}
解决方法是在当前标记和setter之间添加一个.j.
。
您正在使用索引遍历标记数组。因此,未定义标记。您应该通过标记数组中的索引引用标记
function DoClearMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].j.map = null;
markers[i].map = null;
// marker.setMap(null); <= Won't work. ".setMap() is not a function"
//the above won't work because `marker` is not defined.
//instead, reference the marker by its index in the array:
markers[i].setMap(null);
}
markers = [];
}