【问题标题】:AngularJS Leaflet getMap() doesn't workAngularJS Leaflet getMap() 不起作用
【发布时间】:2024-01-10 13:29:02
【问题描述】:

将 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 解决了这个问题。一定是个bug。

【问题讨论】:

  • 我遇到了同样的问题。你举报了吗?
  • 是的,几个月前我做过 - 尚未采取任何行动。
  • 您应该将 标签的 ID 传递给 getMap() 函数,如下所示:leafletData.getMap('theMap').then(...

标签: angularjs leaflet angular-leaflet-directive


【解决方案1】:

您应该将&lt;leaflet&gt; 标签中指定的id 传递给getMap() 函数。

在您的示例中,ID 为map。你会这样传递它:

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

【讨论】:

    最近更新 更多