使用Lat或Long删除Google Marker

时间:2014-05-05 10:28:42

标签: javascript google-maps google-maps-api-3 map google-maps-markers

我想使用Lat或Long值删除Marker。我创建了一个删除标记功能,用于删除地图上存在的标记。

1 个答案:

答案 0 :(得分:0)

从谷歌地图中删除标记时,只需设置setMap(null)

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
         var marker;
         var map;
        function initialize() {
            var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
            var mapOptions = {
                zoom: 4,
                center: myLatlng
            };
           map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
           marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: 'Hello World!'
            });

        }
        function removeMarker(lat,long)
        {
            marker.setMap(null); //no need to set marker again - so removed
        }
        google.maps.event.addDomListener(window, 'load', initialize);

    </script>

<body>
<div style="width: 500px; height: 500px;" id="map-canvas"></div>
<input type="button" onclick="removeMarker(-25.363882, 131.044922)" value="Remove" />
</body>