Skip to content
This repository was archived by the owner on Sep 20, 2019. It is now read-only.

Commit b45df20

Browse files
committed
feat(markers): The icon definition has ben changed to be an object of properties, not a Leaflet Icon object
1 parent 9023bdd commit b45df20

File tree

3 files changed

+48
-65
lines changed

3 files changed

+48
-65
lines changed

src/directives/markers.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,20 @@ angular.module("leaflet-directive").directive('markers', function ($log, $rootSc
4343
// Delete markers from the array
4444
for (var name in leafletMarkers) {
4545
if (!isDefined(newMarkers) || !isDefined(newMarkers[name])) {
46-
deleteMarker(map, leafletMarkers, layers, groups, name);
46+
deleteMarker(map, leafletMarkers[name], layers, groups);
47+
delete leafletMarkers[name];
4748
}
4849
}
4950

5051
// add new markers
5152
for (var newName in newMarkers) {
5253
if (!isDefined(leafletMarkers[newName])) {
5354
var newMarker = createMarker('markers.'+newName, newMarkers[newName], leafletScope, map, layers, groups, shouldWatch);
54-
if (isDefined(newMarker)) {
55-
leafletMarkers[newName] = newMarker;
55+
if (!isDefined(newMarker)) {
56+
$log.error('[AngularJS - Leaflet] Received invalid data on the marker ¡ ' + newName + '.');
57+
continue;
5658
}
59+
leafletMarkers[newName] = newMarker;
5760
}
5861
}
5962
}, shouldWatch);

src/services/leafletMarkersHelpers.js

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,6 @@ angular.module("leaflet-directive").factory('leafletMarkersHelpers', function ($
99
safeApply = leafletHelpers.safeApply,
1010
availableMarkerEvents = leafletEvents.getAvailableMarkerEvents();
1111

12-
var LeafletDefaultIcon = L.Icon.extend({
13-
options: {
14-
iconUrl: 'http://cdn.leafletjs.com/leaflet-0.7/images/marker-icon.png',
15-
iconRetinaUrl: 'http://cdn.leafletjs.com/leaflet-0.7/images/marker-icon-2x.png',
16-
iconSize: [25, 41],
17-
iconAnchor: [12, 40],
18-
labelAnchor: [10, -20],
19-
popupAnchor: [0, -40],
20-
shadow: {
21-
url: 'http://cdn.leafletjs.com/leaflet-0.7/images/marker-shadow.png',
22-
retinaUrl: 'http://cdn.leafletjs.com/leaflet-0.7/images/marker-shadow.png',
23-
size: [41, 41],
24-
anchor: [12, 40]
25-
}
26-
}
27-
});
28-
2912
var hasLabel = function(marker) {
3013
return Helpers.LabelPlugin.isLoaded() && isDefined(marker.label);
3114
};
@@ -38,42 +21,6 @@ angular.module("leaflet-directive").factory('leafletMarkersHelpers', function ($
3821
}
3922
};
4023

41-
var getLeafletIcon = function(data) {
42-
return new LeafletDefaultIcon(data);
43-
};
44-
45-
var buildMarker = function(data) {
46-
if (!isDefined(data)) {
47-
return;
48-
}
49-
50-
var micon = null;
51-
if (data.icon) {
52-
micon = data.icon;
53-
} else {
54-
micon = getLeafletIcon();
55-
}
56-
var moptions = {
57-
icon: micon,
58-
draggable: data.draggable ? true : false,
59-
clickable: isDefined(data.clickable) ? data.clickable : true,
60-
riseOnHover: isDefined(data.riseOnHover) ? data.riseOnHover : false
61-
};
62-
if (data.title) {
63-
moptions.title = data.title;
64-
}
65-
var marker = new L.marker(data, moptions);
66-
67-
if (data.message) {
68-
marker.bindPopup(data.message);
69-
}
70-
if (Helpers.LabelPlugin.isLoaded() && isDefined(data.label) && isDefined(data.label.message)) {
71-
marker.bindLabel(data.label.message, data.label.options);
72-
}
73-
74-
return marker;
75-
};
76-
7724
var genDispatchEventCB = function(eventName, logic, scope_watch_name, leafletScope, marker, marker_data) {
7825
return function(e) {
7926
var broadcastName = 'leafletDirectiveMarker.' + eventName;
@@ -112,19 +59,50 @@ angular.module("leaflet-directive").factory('leafletMarkersHelpers', function ($
11259
};
11360
};
11461

62+
var getLeafletIcon = function(iconData) {
63+
if (!isDefined(iconData)) {
64+
return new L.Icon.Default();
65+
}
66+
return new L.Icon.Default(iconData);
67+
};
68+
69+
var buildMarker = function(data) {
70+
if (!isDefined(data)) {
71+
return;
72+
}
73+
var markerOptions = {
74+
icon: getLeafletIcon(data.icon),
75+
draggable: isDefined(data.draggable) ? data.draggable : false,
76+
clickable: isDefined(data.clickable) ? data.clickable : true,
77+
riseOnHover: isDefined(data.riseOnHover) ? data.riseOnHover : false
78+
};
79+
if (isDefined(data.title)) {
80+
markerOptions.title = data.title;
81+
}
82+
var marker = new L.marker(data, markerOptions);
83+
84+
if (isDefined(data.message)) {
85+
marker.bindPopup(data.message);
86+
}
87+
if (Helpers.LabelPlugin.isLoaded() && isDefined(data.label) && isDefined(data.label.message)) {
88+
marker.bindLabel(data.label.message, data.label.options);
89+
}
90+
91+
return marker;
92+
};
93+
11594
return {
11695
getLeafletIcon: getLeafletIcon,
11796

118-
deleteMarker: function(map, leafletMarkers, layers, groups, name) {
119-
var marker = leafletMarkers[name];
120-
97+
deleteMarker: function(map, marker, layers, groups) {
12198
// There is no easy way to know if a marker is added to a layer, so we search for it
12299
// if there are overlays
123100
if (isDefined(layers) && isDefined(layers.overlays)) {
124101
for (var key in layers.overlays) {
125102
if (layers.overlays[key] instanceof L.LayerGroup) {
126103
if (layers.overlays[key].hasLayer(marker)) {
127104
layers.overlays[key].removeLayer(marker);
105+
continue;
128106
}
129107
}
130108
}
@@ -134,18 +112,19 @@ angular.module("leaflet-directive").factory('leafletMarkersHelpers', function ($
134112
for (var groupKey in groups) {
135113
if (groups[groupKey].hasLayer(marker)) {
136114
groups[groupKey].removeLayer(marker);
115+
continue;
137116
}
138117
}
139118
}
140119

141120
map.removeLayer(marker);
142-
delete leafletMarkers[name];
143121
},
144122

145123
createMarker: function(scope_watch_name, marker_data, leafletScope, map, layers, groups, shouldWatch) {
146124
var marker = buildMarker(marker_data);
147125

148126
if (!isDefined(marker)) {
127+
$log.error('[AngularJS - Leaflet] The marker definition is not valid.');
149128
return;
150129
}
151130

test/unit/markersDirectiveSpec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,24 +463,24 @@ describe('Directive: leaflet', function() {
463463
var DEFAULT_URL = 'http://cdn.leafletjs.com/leaflet-0.5.1/images/marker-icon.png';
464464

465465
beforeEach(function(){
466-
leafIcon = L.icon({
466+
leafIcon = {
467467
iconUrl: LEAF_URL,
468468
shadowUrl: 'http://leafletjs.com/docs/images/leaf-shadow.png',
469469
iconSize: [38, 95],
470470
shadowSize: [50, 64],
471471
iconAnchor: [22, 94],
472472
shadowAnchor: [4, 62],
473473
popupAnchor: [-3, -76]
474-
});
475-
defaultIcon = L.icon({
474+
};
475+
defaultIcon = {
476476
iconUrl: DEFAULT_URL,
477477
shadowUrl: 'http://cdn.leafletjs.com/leaflet-0.5.1/images/marker-shadow.png',
478478
iconSize: [25, 41],
479479
iconAnchor: [12, 40],
480480
popupAnchor: [0, 40],
481481
shadowSize: [41, 41],
482482
shadowAnchor: [12, 40]
483-
});
483+
};
484484

485485
mainMarkers = {
486486
m1: {
@@ -503,7 +503,8 @@ describe('Directive: leaflet', function() {
503503
});
504504

505505
scope.$digest();
506-
expect(markers.m1.options.icon.options.iconUrl).toEqual(LEAF_URL);
506+
var icon = markers.m1.options.icon;
507+
expect(icon.options.iconUrl).toEqual(LEAF_URL);
507508

508509
mainMarkers.m1.icon = defaultIcon;
509510
scope.$apply();

0 commit comments

Comments
 (0)