【问题标题】:Can't remove markers with AngularJS Google Maps plugin and Ionic无法使用 AngularJS Google Maps 插件和 Ionic 删除标记
【发布时间】:2015-05-30 16:15:21
【问题描述】:

我的 html 页面通过以下方式呈现地图:

<ui-gmap-google-map center='map.center' zoom='map.zoom'>
  <ui-gmap-markers models="markers" coords="'coords'" icon="'icon'" ></ui-gmap-markers>
</ui-gmap-google-map>

这是控制器:

angular.module('controllers.mapscontroller', [])

.controller("MapCtrl", function($scope, uiGmapGoogleMapApi, dataFactory) {

    $scope.POIs=[];
    $scope.markers = [];

    uiGmapGoogleMapApi.then(function(maps) {

        $scope.map = { center: { latitude: 40.3555013, longitude: 18.1573811 }, zoom: 8 };

        navigator.geolocation.getCurrentPosition(function (position) {
            $scope.map = { center: { latitude: position.coords.latitude, longitude: position.coords.longitude }, zoom: 8 };
        });

        dataFactory.getPOIs("it-IT")
            .success(function (data) {
                $scope.POIs=data.data.contents;
                $scope.initializePOIsOnMap();
            })
            .error(function (error) {
                console.log(error);
            });
    });

    $scope.removePOIs = function() {

        $scope.markers=[]; //<-- does nothing !!!
    };


    $scope.initializePOIsOnMap = function() {

        for(var POI in $scope.POIs) {

            var marker = {
              id: $scope.POIs[POI].id,
              coords: {
                latitude: $scope.POIs[POI].latitude,
                longitude: $scope.POIs[POI].longitude
              },
              options: { draggable: false , visible:true},
              icon: "img/catIcons/"+$scope.POIs[POI].catid+".png"
            };

            $scope.markers.push(marker);
        }
    }
});

地图可以正确显示,标记也可以正确显示。 但不幸的是,正如您在 $scope.removePOIs 函数中看到的那样,我无法从地图中删除标记。我尝试了在 stackoverflow 和 Web 上找到的不同解决方案($scope.markers.length=0, marker.setMap(null), ...),但没有运气。

有什么提示吗?? 谢谢 罗伯托

【问题讨论】:

    标签: angularjs google-maps marker ionic


    【解决方案1】:

    下面是一个使用 angularjs 删除标记的例子:

    Example

    查看删除标记示例的脚本。

    【讨论】:

    【解决方案2】:

    解决了!! 问题是我两次指定了控制器:在 app.js 和 HTML 页面中。在 HTML 页面中指定它的原因是我使用了 switchery 插件,它需要在标签中设置 ng-controller 属性。 我从 app.js 中删除了控制器属性,将所有 HTML(地图标签)放入标签中,现在它可以工作了。 谢谢! 罗伯托

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-15
      • 2015-07-16
      • 2021-12-26
      • 2012-07-20
      • 2012-05-24
      • 2014-08-04
      • 2013-05-02
      • 1970-01-01
      相关资源
      最近更新 更多