从 google 地图 xml 中移除标记
Removing markers from google maps xml
我正在尝试在定期刷新的 Google 地图上显示标记。
我找到了这段代码,可以将它们添加到地图中,但我需要知道如何在每次刷新时删除它们,以便它们停止在地图上显示不再在 xml 文件中。
< script >
var map = null;
function initialize() {
var mapOptions = {
zoom: 12,
center: new google.maps.LatLng(53.3478, -6.2597) //center over dublin
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
loadXMLFile();
}
function loadXMLFile() {
var filename = 'data.xml';
$.ajax({
type: "GET",
url: filename,
dataType: "xml",
success: parseXML,
error: onXMLLoadFailed
});
function onXMLLoadFailed() {
alert("An Error has occurred.");
}
function parseXML(xml) {
var bounds = new google.maps.LatLngBounds();
$(xml).find("marker").each(function() {
//Read the name, address, latitude and longitude for each Marker
var nme = $(this).find('name').text();
var address = $(this).find('address').text();
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var markerCoords = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
bounds.extend(markerCoords);
var marker = new google.maps.Marker({
position: markerCoords,
map: map
});
});
map.fitBounds(bounds);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
< /script>
这在通过调用函数 loadXMLFile() 添加新标记时效果很好,但如果它们被删除,它们就会保留在地图上。我想我需要删除它们 https://developers.google.com/maps/documentation/javascript/examples/marker-remove 但我不知道如何应用它。
这几天一直让我抓狂。谁能帮帮我?
编辑:
我的完整代码没有渲染地图:
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
#map-canvas {
height: 400px;
margin-bottom: 10px;
} }
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
var map;
var markers = [];
function initialize() {
var mapOptions = {
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(59.76522, 18.35002)
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
google.maps.event.addDomListener(document.getElementById('add-markers'), 'click', addMarkers);
google.maps.event.addDomListener(document.getElementById('remove-markers'), 'click', removeMarkers);
}
function addMarkers() {
var coords = [
new google.maps.LatLng(59.32522, 18.07002),
new google.maps.LatLng(59.45522, 18.12002),
new google.maps.LatLng(59.86522, 18.35002),
new google.maps.LatLng(59.77522, 18.88002),
new google.maps.LatLng(59.36344, 18.36346),
new google.maps.LatLng(59.56562, 18.33002)];
for (var i = 0; i < coords.length; i++) {
var marker = new google.maps.Marker({
map: map,
position: coords[i]
});
markers.push(marker);
}
}
function removeMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
}
initialize();
</script>
</head>
<body>
<div id="map-canvas"></div>
<button id="add-markers">Add markers</button>
<button id="remove-markers">Remove markers</button>
</body>
</html>
1) 创建一个数组来保存你的标记
var markers = [];
2) 将每个标记添加到地图时将它们推送到数组
var marker = new google.maps.Marker({
position: markerCoords,
map: map
});
markers.push(marker);
3) 当您需要删除它们时,遍历您的数组并在每个标记上调用 setMap(null)
for (var i=0; i<markers.length; i++) {
markers[i].setMap(null);
}
希望对您有所帮助。
编辑:
这是您的代码,经过我的修改后更新。我猜你 定期 调用 loadXMLFile()
函数 (?)。
var map = null;
var markers = [];
function initialize() {
var mapOptions = {
zoom: 12,
center: new google.maps.LatLng(53.3478, -6.2597) //center over dublin
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
loadXMLFile();
}
function loadXMLFile() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
var filename = 'data.xml';
$.ajax({
type: "GET",
url: filename,
dataType: "xml",
success: parseXML,
error: onXMLLoadFailed
});
function onXMLLoadFailed() {
alert("An Error has occurred.");
}
function parseXML(xml) {
var bounds = new google.maps.LatLngBounds();
$(xml).find("marker").each(function () {
//Read the name, address, latitude and longitude for each Marker
var nme = $(this).find('name').text();
var address = $(this).find('address').text();
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var markerCoords = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
bounds.extend(markerCoords);
var marker = new google.maps.Marker({
position: markerCoords,
map: map
});
markers.push(marker);
});
map.fitBounds(bounds);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
我正在尝试在定期刷新的 Google 地图上显示标记。
我找到了这段代码,可以将它们添加到地图中,但我需要知道如何在每次刷新时删除它们,以便它们停止在地图上显示不再在 xml 文件中。
< script >
var map = null;
function initialize() {
var mapOptions = {
zoom: 12,
center: new google.maps.LatLng(53.3478, -6.2597) //center over dublin
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
loadXMLFile();
}
function loadXMLFile() {
var filename = 'data.xml';
$.ajax({
type: "GET",
url: filename,
dataType: "xml",
success: parseXML,
error: onXMLLoadFailed
});
function onXMLLoadFailed() {
alert("An Error has occurred.");
}
function parseXML(xml) {
var bounds = new google.maps.LatLngBounds();
$(xml).find("marker").each(function() {
//Read the name, address, latitude and longitude for each Marker
var nme = $(this).find('name').text();
var address = $(this).find('address').text();
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var markerCoords = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
bounds.extend(markerCoords);
var marker = new google.maps.Marker({
position: markerCoords,
map: map
});
});
map.fitBounds(bounds);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
< /script>
这在通过调用函数 loadXMLFile() 添加新标记时效果很好,但如果它们被删除,它们就会保留在地图上。我想我需要删除它们 https://developers.google.com/maps/documentation/javascript/examples/marker-remove 但我不知道如何应用它。
这几天一直让我抓狂。谁能帮帮我?
编辑:
我的完整代码没有渲染地图:
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
#map-canvas {
height: 400px;
margin-bottom: 10px;
} }
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
var map;
var markers = [];
function initialize() {
var mapOptions = {
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(59.76522, 18.35002)
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
google.maps.event.addDomListener(document.getElementById('add-markers'), 'click', addMarkers);
google.maps.event.addDomListener(document.getElementById('remove-markers'), 'click', removeMarkers);
}
function addMarkers() {
var coords = [
new google.maps.LatLng(59.32522, 18.07002),
new google.maps.LatLng(59.45522, 18.12002),
new google.maps.LatLng(59.86522, 18.35002),
new google.maps.LatLng(59.77522, 18.88002),
new google.maps.LatLng(59.36344, 18.36346),
new google.maps.LatLng(59.56562, 18.33002)];
for (var i = 0; i < coords.length; i++) {
var marker = new google.maps.Marker({
map: map,
position: coords[i]
});
markers.push(marker);
}
}
function removeMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
}
initialize();
</script>
</head>
<body>
<div id="map-canvas"></div>
<button id="add-markers">Add markers</button>
<button id="remove-markers">Remove markers</button>
</body>
</html>
1) 创建一个数组来保存你的标记
var markers = [];
2) 将每个标记添加到地图时将它们推送到数组
var marker = new google.maps.Marker({
position: markerCoords,
map: map
});
markers.push(marker);
3) 当您需要删除它们时,遍历您的数组并在每个标记上调用 setMap(null)
for (var i=0; i<markers.length; i++) {
markers[i].setMap(null);
}
希望对您有所帮助。
编辑:
这是您的代码,经过我的修改后更新。我猜你 定期 调用 loadXMLFile()
函数 (?)。
var map = null;
var markers = [];
function initialize() {
var mapOptions = {
zoom: 12,
center: new google.maps.LatLng(53.3478, -6.2597) //center over dublin
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
loadXMLFile();
}
function loadXMLFile() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
var filename = 'data.xml';
$.ajax({
type: "GET",
url: filename,
dataType: "xml",
success: parseXML,
error: onXMLLoadFailed
});
function onXMLLoadFailed() {
alert("An Error has occurred.");
}
function parseXML(xml) {
var bounds = new google.maps.LatLngBounds();
$(xml).find("marker").each(function () {
//Read the name, address, latitude and longitude for each Marker
var nme = $(this).find('name').text();
var address = $(this).find('address').text();
var lat = $(this).find('lat').text();
var lng = $(this).find('lng').text();
var markerCoords = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
bounds.extend(markerCoords);
var marker = new google.maps.Marker({
position: markerCoords,
map: map
});
markers.push(marker);
});
map.fitBounds(bounds);
}
}
google.maps.event.addDomListener(window, 'load', initialize);