Ich versuche, Marker aus der Karte zu löschen, die mit Google Map API erstellt wird. Es sieht so aus, als ob ich keine GM API verwende und diese Funktion ist undefiniert, sonst tue ich das. Was ist falsch? Können Sie mir dabei helfen, da dies das erste Mal ist, dass ich eine API verwende?setMap() ist undefined
var myLatlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
function Point(index, latitude, longitude) {
this.index = index;
this.latitude = latitude;
this.longitude = longitude;
}
map.markers = [];
function DeleteMarker(index) {
console.log(JSON.stringify(map.markers[index]));
map.markers[index].setMap(null); ///////PROBLEM IS HERE
map.markers[index] = null;
map.markers.splice(index, 1);
}
google.maps.event.addListener(map, 'click', function (event) {
placeMarker(event.latLng);
});
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
animation: google.maps.Animation.DROP,
map: map
});
var index = map.markers.length;
map.markers.push(new Point(map.markers.length, location.lat(), location.lng()));
console.log(JSON.stringify(map.markers.length));
var infowindow = new google.maps.InfoWindow({
content: '<p>Details:'
+ '<p>Latitude:' + location.lat() + '</p>'
+ '<p>Longitude:' + location.lng() + '</p>' +
'<button id ="btnDeleteMarker" onclick=DeleteMarker(\'' + index + '\')>Delete this stop</button>'
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
}
Ist der Fehler, die Sie bekommen, "nicht Eigentum setMap undefinierter lesen kann"? –
@JarodMoser der Fehler ist, dass ich eine Nachricht bekam, setMap() ist keine Funktion und die Ursache dafür ist, dass es nicht definiert ist – Ekaterina
Was ist der Wert von 'index', wenn Sie den Fehler erhalten? Wie lang ist das map.markers-Array? Geben Sie bitte. [mcve] das zeigt das Problem. – geocodezip