I have 2 function, 1 to add markers and 1 to remove markers.

What I am doing to add markers

$scope.getMapData = function (msg, passedIndex) {

msg = L.layerGroup();
for (var i= 0; i < $scope.apiResult.length; i++) {
if ($scope.apiResult[i].Type == "Point"){
$scope.dataLatLng.push($scope.apiResult[i].LatLng)
$scope.Lat.push($scope.dataLatLng[i].split(',')[0]);
$scope.Lng.push($scope.dataLatLng[i].split(',')[1]);
L.marker([$scope.Lat[i], $scope.Lng[i]], {icon: greenIcon}).bindPopup($scope.apiResult[i].DESCRIPTION).addTo(msg);
   }
       map.addLayer(msg);
       if(map.hasLayer(msg)){
       console.log('why')
      }
}

Remove marker:

        $scope.removeMarker = function (msg, passedIndex) {
          if(map.hasLayer(msg)){
            console.log('why')
          }
          absolute_index = passedIndex + ($scope.currentPage) * $scope.pageSize;
          if($scope.community[absolute_index].visibility)
            {

              $scope.community[absolute_index].visibility = false;
              // cities.removeLayer(a);
            } else {
               $scope.community[absolute_index].visibility = true;
            }
        }

When I console log map.hasLayer in my "add marker" function, it returns me the value, but when I console log that in my remove marker function, i am not returned anything.

PS: My "msg" all has a unique name like "express_speed". I tried using removeLayer on msg but it doesn't work.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.