我需要删除之前添加的标记,而无需刷新屏幕,过去2天尝试了很多代码,但无法执行。这个问题听起来像是重复的,是的,但是就我而言,我无法修复该错误,任何建议和帮助都将非常有用。我在下面附加了我的代码。谢谢。 P.S:我是google maps的新手(基于C#代码,javascript,asp.net mvc,ms sql db构建的项目)
var map = new GMaps({
el: '#map',
lat: 11.0168,
lng: 76.9558
});
geolocate();
function geolocate() {
$.ajax({
url: '@Url.Action("loadmap", "LiveTracking")',
type: "Get",
dataType: 'json',
success: function (results) {
debugger;
var result = JSON.parse(results);
var lat = parseFloat(result[0].Latitude)
var lng = parseFloat(result[0].Longitude)
var VehicleId = result[0].VehicleID;
var Speed = result[0].Speed;
var abc = ("Vehicle:" + VehicleId + " , " + "Speed:" + Speed )
map.addMarker({
lat: lat,
lng: lng,
icon: {
url: '/Content/Images/zaz.png',
scaledSize: new google.maps.Size(65, 65),
},
title: 'vehicle',
infoWindow: {
content: abc
},
});
// }
}
})
}
Most commonly suggested method was to add an marker[] and push in it and call the delete function ,which I tried but can't able to achieve it.
希望任何人都可以帮助我,提前感谢
答案 0 :(得分:0)
您看过本文档吗?
https://developers.google.com/maps/documentation/javascript/examples/marker-remove
在设置空数组之前,它具有clearMarkers()。
function deleteMarkers() {
clearMarkers();
markers = [];
}
function clearMarkers() {
setMapOnAll(null);
}
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
答案 1 :(得分:0)
<body onload="googleMap()">
<div id="map_canvas" style="height: 300px; width: 300px"></div>
<a onclick="removeMarker()">Remove marker</a>
<script type="text/javascript">
var map, myLatLng, marker;
function googleMap() {
var mapOptions = {
center: new google.maps.LatLng(52.645813,-0.382000),
zoom: 15,
scrollwheel: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
myLatLng = new google.maps.LatLng(52.645945,-0.382837);
marker = new google.maps.Marker({ position: myLatLng, map: map });
};
function removeMarker() {
marker.setMap(null);
};
</script>
</body>