I'm building an app in AngularJS that uses LeafletJS for interacting with a map, offering different possible interactions separated across what I call phases. For each of those phases there is a UIRouter
state, with its controller and template.
I'm currently providing the leaflet functionality through a service. The idea was for that service to initialise a Leaflet map and provide some limited access to the state's controller. Those controllers would thus call service functions such as setupMarkersInteractions
to setup callbacks that enable marker placement on the map, for example.
However, I'm running into a problem when initialising the map through Leaflet's leaflet.map()
function, namely: Error: Map container not found
. This is related to Leaflet's inability to find the HTML element with which the map should be associated.
Currently, I'm kinda doing this:
function mapService() {
var map;
return {
initializeMap : initializeMap,
setupMarkersInteractions : setupMarkersInteractions
};
function initializeMap() {
map = leaflet.map('map');
}
function setupMarkersInteractions() {
map.on('click', markerPlacementCallback);
}
}
The initializeMap
function tells leaflet.map
to look for a HTML element with id='map'
, which is declared on the state's template.
Now, for the actual question, is this related to some kind of AngularJS services' inability to access the HTML template? I couldn't find anything on the matter, but I thought that it would make sense for services to not directly access the view...
If it is, what kind of workaround should I explore? I've looked into leaflet-directive
, but it doesn't seem to offer the possibility to add and remove custom callbacks with the flexibility I would like to (things get complex when I add free draw functionality with Leaflet-Freedraw, for example).
I considered using leaflet.map
directly with an HTMLElement
argument for the element but still I couldn't make it work - although there is a probability that I'm not passing what is supposed to.