Question

I want remove a existing marker using marker's lat or long . I have a button which is call remove function . The remove function will remove the marker as a lat or long which is define but method is not working

<!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>
Was it helpful?

Solution

You may store the markers somewhere (e.g. in a object) and use the latLng as key:

     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) {}
    }

Demo: http://jsfiddle.net/3qw8d/3/

OTHER TIPS

You have to store marker in another object with reference of your latitude and longitude.

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);
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top