2014-07-22 20 views
5

加入傳單到我的AngularJS應用後:AngularJS傳單的GetMap()不工作

<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解決了這個問題。必須是一個錯誤。

+0

我有同樣的問題。你有報告嗎? – schmijos

+0

是的,我在幾個月前 - 沒有采取任何行動。 – Julius

+0

您應該通過標記的ID來獲取像這樣的Map()函數:leafletData.getMap('theMap')。then(... – Dojo

回答

16

您應該通過<leaflet>標記中指定的id以獲取Map()函數。

在你的例子中,ID是map。你會通過這樣的:

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