Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to remove markers from Google Maps API?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.13k
    Comment on it

    Remove markers from Google MAPS API v3:

    I was facing issues while removing all the markers from the Google map API. I know how to remove markers in Google Maps API v2 by :

    map.clearOverlays();

    Now the question is how to remove markers from Google maps API v3. This is the following lines of code will solve tis issue:

    var map;
    var markersArray = [];
    
    function initialize() {
      var haightAshbury = new google.maps.LatLng(37.7699298, -122.4469157);
      var mapOptions = {
        zoom: 12,
        center: haightAshbury,
        mapTypeId: google.maps.MapTypeId.TERRAIN
      };
      map =  new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    
      google.maps.event.addListener(map, 'click', function(event) {
        addMarker(event.latLng);
      });
    }
    
    function addMarker(location) {
      marker = new google.maps.Marker({
        position: location,
        map: map
      });
      markersArray.push(marker);
    }
    
    // Removes the overlays from the map, but keeps them in the array
    function clearOverlays() {
      if (markersArray) {
        for (i in markersArray) {
          markersArray[i].setMap(null);
        }
      }
    }
    
    // Shows any overlays currently in the array
    function showOverlays() {
      if (markersArray) {
        for (i in markersArray) {
          markersArray[i].setMap(map);
        }
      }
    }
    
    // Deletes all markers in the array by removing references to them
    function deleteOverlays() {
      if (markersArray) {
        for (i in markersArray) {
          markersArray[i].setMap(null);
        }
        markersArray.length = 0;
      }
    }

    Thats all need to be done!

    Thanks for reading the blog

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: