To Remove Previous Markers Before Adding New Markers
I am trying to develop Location based web application .I am facing problem to remove previous markers before adding new markers into the google  maps. the map uses click event to a
Solution 1:
I did the same thing in one of my projects. Works smoothly!
var gmarkers = []; //declared globallyfunction removeMarkers() {
   for (i = 0; i < gmarkers.length; i++) {
    gmarkers[i].setMap(null);
   }
 }
While inserting the markers, I used to insert the marker in array
gmarkers.push(marker);
call removeMarkers() based on your requirements.
Solution 2:
I did this but in Angular version works :)
http://plnkr.co/edit/caW6vr?p=preview
angular.module('nowCtrls', []).controller('NowCtrl', function() {
  var nc = this;
  nc.positions = [];
  nc.placeMarker = function(event) {
    nc.positions = [];
    var loc = event.latLng;
    nc.positions.push({
      pos: [loc.lat(), loc.lng()]
    });
  };
}).controller('BuyCtrl', function() {
});
Post a Comment for "To Remove Previous Markers Before Adding New Markers"