ngplayground
ngplayground

Reputation: 21617

AngularJS & Google Map API V3 remove marker from map

I'm rattling my brains here and cannot figure out why this is not working

var lat = Math.round(top_location.geometry.location.lat() * 1000000)/1000000;
var lng = Math.round(top_location.geometry.location.lng() * 1000000)/1000000;
geocode_results[i]['lat'] = lat;
geocode_results[i]['lng'] = lng;
geocode_results[i]['l_type'] = top_location.geometry.location_type;
marker = new google.maps.Marker({
    icon: mapIcon,
    position: new google.maps.LatLng(lat,lng),
    map: map
});
markersArray.push(top_location.address_components[0].long_name);

Using the above created my marker(s) and plots them on my map.

Using the following code to remove the markers from the map

$scope.removemarkers = function() {
    console.log($scope);
    console.log(markersArray);
    if (markersArray && markersArray.length) {
        for (var i = 0; i < markersArray.length; i++) {
            markersArray[i].setMap(null);
        }
        markersArray = [];
    }
};

I get the following error in console.log()

TypeError: Object AB42 2DL has no method 'setMap'at Object.$scope.removemarkers

AB42 2DL being a random postcode used when plotting a marker

Upvotes: 0

Views: 878

Answers (1)

Gruff Bunny
Gruff Bunny

Reputation: 27976

markersArray doesn't contain markers, it contains strings.

Try this:

markersArray.push(marker);

Upvotes: 4

Related Questions