This repository was archived by the owner on Sep 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathleaflet.js
151 lines (131 loc) · 5.38 KB
/
leaflet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
angular.module('ui-leaflet', ['nemLogging']).directive('leaflet',
function ($q, leafletData, leafletMapDefaults, leafletHelpers, leafletMapEvents) {
return {
restrict: "EA",
replace: true,
scope: {
center : '=',
lfCenter : '=',
defaults : '=',
maxbounds : '=',
bounds : '=',
markers : '=',
legend : '=',
geojson : '=',
paths : '=',
tiles : '=',
layers : '=',
controls : '=',
decorations : '=',
eventBroadcast : '=',
watchOptions : '=',
id : '@'
},
transclude: true,
template: '<div class="angular-leaflet-map"><div ng-transclude></div></div>',
controller: function ($scope) {
this._leafletMap = $q.defer();
this.getMap = function () {
return this._leafletMap.promise;
};
this.getLeafletScope = function() {
return $scope;
};
},
link: function(scope, element, attrs, ctrl) {
var isDefined = leafletHelpers.isDefined,
defaults = leafletMapDefaults.setDefaults(scope.defaults, attrs.id),
mapEvents = leafletMapEvents.getAvailableMapEvents(),
addEvents = leafletMapEvents.addEvents;
scope.mapId = attrs.id;
leafletData.setDirectiveControls({}, attrs.id);
// Set width and height utility functions
function updateWidth() {
if (isNaN(attrs.width)) {
element.css('width', attrs.width);
} else {
element.css('width', attrs.width + 'px');
}
}
function updateHeight() {
if (isNaN(attrs.height)) {
element.css('height', attrs.height);
} else {
element.css('height', attrs.height + 'px');
}
}
// Create the Leaflet Map Object with the options
var map = new L.Map(element[0], leafletMapDefaults.getMapCreationDefaults(attrs.id));
ctrl._leafletMap.resolve(map);
// If the width attribute defined update css
// Then watch if bound property changes and update css
if (isDefined(attrs.width)) {
updateWidth();
scope.$watch(
function () {
return element[0].getAttribute('width');
},
function () {
updateWidth();
map.invalidateSize();
});
}
// If the height attribute defined update css
// Then watch if bound property changes and update css
if (isDefined(attrs.height)) {
updateHeight();
scope.$watch(
function () {
return element[0].getAttribute('height');
},
function () {
updateHeight();
map.invalidateSize();
});
}
if (!isDefined(attrs.center) && !isDefined(attrs.lfCenter)) {
map.setView([defaults.center.lat, defaults.center.lng], defaults.center.zoom);
}
// If no layers nor tiles defined, set the default tileLayer
if (!isDefined(attrs.tiles) && (!isDefined(attrs.layers))) {
var tileLayerObj = L.tileLayer(defaults.tileLayer, defaults.tileLayerOptions);
tileLayerObj.addTo(map);
leafletData.setTiles(tileLayerObj, attrs.id);
}
// Set zoom control configuration
if (isDefined(map.zoomControl) &&
isDefined(defaults.zoomControlPosition)) {
map.zoomControl.setPosition(defaults.zoomControlPosition);
}
if (isDefined(map.zoomControl) &&
defaults.zoomControl===false) {
map.zoomControl.removeFrom(map);
}
if (isDefined(map.zoomsliderControl) &&
isDefined(defaults.zoomsliderControl) &&
defaults.zoomsliderControl===false) {
map.zoomsliderControl.removeFrom(map);
}
// if no event-broadcast attribute, all events are broadcasted
if (!isDefined(attrs.eventBroadcast)) {
var logic = "broadcast";
addEvents(map, attrs.id, mapEvents, "eventName", scope, logic);
}
// Resolve the map object to the promises
map.whenReady(function() {
leafletData.setMap(map, attrs.id);
});
scope.$on('$destroy', function () {
leafletMapDefaults.reset(attrs.id);
map.remove();
leafletData.unresolveMap(attrs.id);
});
//Handle request to invalidate the map size
//Up scope using $scope.$emit('invalidateSize')
//Down scope using $scope.$broadcast('invalidateSize')
scope.$on('invalidateSize', function() {
map.invalidateSize();
});
}
};
});