gpt4 book ai didi

AngularJS Leaflet getMap() 不起作用

转载 作者:行者123 更新时间:2023-12-02 21:32:25 27 4
gpt4 key购买 nike

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

<leaflet id="map" defaults="defaults" center="center" bounds="bounds" controls="controls" event-broadcast="events"></leaflet>

并进行设置:

// 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']
}
}
});

按照此代码,它不会被评估(但没有显示错误):

leafletData.getMap().then(
function (map) {
alert('I have accessed the map.');
}
);

这应该立即向我显示警报,尽管什么也没有发生。

如果我延迟前面的代码,例如,在单击按钮时在函数中运行它,它就会起作用!

有人知道可能出现什么问题吗?

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

部分解决

leaflet HTML 标记中删除 ID 解决了该问题。一定是一个错误。

最佳答案

您应该通过id <leaflet>中指定标记到 getMap() 函数。

在您的示例中,ID 是 map 。你可以像这样传递它:

leafletData.getMap("map").then(
function (map) {
alert('I have accessed the map.');
}
);

关于AngularJS Leaflet getMap() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24891432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com