我为地图中的特定位置添加了一些标记,我有两个按钮,一个用于隐藏所有标记(clearOverlays
),另一个用于再次显示它们(showOverlays
)但它不是不工作在我的JavaScript控制台中,我收到此错误:Cannot call method 'setMap' of undefined
这是我的代码..
// COORDENADAS
var coordenadas = [
['Punto 1', 19.558061,-99.296344, 4],
['Punto 2', 19.55886, -99.296886, 5],
['Punto 3',19.559103, -99.296688, 3],
['Punto 4', 19.560164,-99.297347, 2],
['Punto 5',19.560073,-99.296064, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: new google.maps.LatLng( 19.561883, -99.298287),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var ventana = new google.maps.InfoWindow();
var marcador, i;
for (i = 0; i < coordenadas.length; i++) {
marcador = new google.maps.Marker
({
position: new google.maps.LatLng(coordenadas[i][1], coordenadas[i][2]),
animation: google.maps.Animation.DROP,
map: map
});
google.maps.event.addListener(
marcador, 'mouseover', (function(marcador, i) {
return function() {
ventana.setContent(coordenadas[i][0]);
ventana.open(map, marcador);
}
})(marcador, i)
);
}
function clearOverlays(map) {
for (var i = 0; i < coordenadas.length; i++) {
marcador[i].setMap(null);
}
setAllMap(null);
console.log('click Ocultar');
}
function showOverlays(map) {
for (var i = 0; i < coordenadas.length; i++) {
marcador[i].setMap(map);
}
setAllMap(map);
console.log('click Mostar');
}
答案 0 :(得分:1)
marcador不是阵列。 marcador [i]不存在。这会给出错误消息:Cannot call method 'setMap' of undefined
您需要一组google.maps.Marker对象。
var gmarkers = [];
function clearOverlays(map) {
for (var i = 0; i < gmarkers.length; i++) {
gmarkers[i].setMap(null);
}
}
function showOverlays(map) {
for (var i = 0; i < gmarkers.length; i++) {
gmarkers[i].setMap(map);
}
}
for (i = 0; i < coordenadas.length; i++) {
var marcador = new google.maps.Marker
({
position: new google.maps.LatLng(coordenadas[i][1], coordenadas[i][2]),
animation: google.maps.Animation.DROP,
map: map
});
gmarkers.push(marcador);
google.maps.event.addListener(
marcador, 'mouseover', (function(marcador, i) {
return function() {
ventana.setContent(coordenadas[i][0]);
ventana.open(map, marcador);
}
})(marcador, i)
);
}