我想使用标记的lat或long删除现有标记。我有一个按钮,它是呼叫删除功能。 remove函数会将标记移除为lat或long,这是定义但方法不起作用
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple markers</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Hello World!'
});
}
function remove()
{
var lastMarker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(-25.363882, 131.044922)
});
lastMarker.setMap(null);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div style="width: 500px; height: 500px;" id="map-canvas"></div>
<input type="button" onclick="remove()" value="Remove" />
</body>
</html>
答案 0 :(得分:3)
您可以将标记存储在某处(例如在对象中)并使用latLng作为键:
var markers = {};
/*......*/
markers[myLatlng] = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Hello World!'
});
/*.......*/
function removeMarker(lat, long) {
try {
markers[new google.maps.LatLng(lat, long)].setMap(null);
} catch (e) {}
}
答案 1 :(得分:1)
您必须参考纬度和经度将标记存储在另一个对象中。
var MARKERS = [];//custom object
/** add marker **/
var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Hello World!'
});
//add marker to MARKERS object
MARKERS[myLatlng] = marker;
/** remove marker **/
function remove()
{
var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
MARKERS[myLatlng].setMap(null);
}