AngularJS Leaflet getMap()不起作用

Jul*_*ius 5 angularjs leaflet angular-leaflet-directive

将Leaflet添加到我的AngularJS应用程序后:

<leaflet id="map" defaults="defaults" center="center" bounds="bounds" controls="controls" event-broadcast="events"></leaflet>
Run Code Online (Sandbox Code Playgroud)

并设置:

// Initialise the feature group to store editable layers
var drawnItems = new L.FeatureGroup();

// Initialise the draw control
var drawControl = new L.Control.Draw({
    position: 'topright',
    edit: {
        featureGroup: drawnItems
    }
});

// Configure Leaflet
angular.extend($scope, {
    defaults: {
        zoomControlPosition: 'topright',
        minZoom: 3,
        tileLayerOptions: {
            detectRetina: true,
            reuseTiles: true,
            attribution: '<a href="http://osm.org">OpenStreetMaps</a>'
        }
    },
    center: {},
    controls: {
        custom: [drawControl]
    },
    events: {
        map: {
            enable: ['click']
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

遵循此代码,它不会被评估(尽管没有显示错误):

leafletData.getMap().then(
    function (map) {
        alert('I have accessed the map.');
    }
);
Run Code Online (Sandbox Code Playgroud)

尽管没有任何反应,这应该会立即向我显示警报.

如果我延迟这个以前的代码,例如,在单击按钮的函数中运行它,它就可以了!

有谁知道什么可能是一个问题?

看到示例,它应该工作:https://github.com/tombatossals/angular-leaflet-directive/blob/master/examples/control-draw-example.html

部分解决

leafletHTML标记中删除ID 解决了这个问题.一定是个bug.

Doj*_*ojo 18

您应该id<leaflet>标记中指定的内容传递给getMap()函数.

在您的示例中,id为map.你会像这样传递它:

leafletData.getMap("map").then(
    function (map) {
        alert('I have accessed the map.');
    }
);
Run Code Online (Sandbox Code Playgroud)