Skip to content

Commit f9e9653

Browse files
committed
Everything working like the last stable version
1 parent 1772e60 commit f9e9653

17 files changed

+1056
-983
lines changed

dist/angular-leaflet-directive.js

Lines changed: 137 additions & 135 deletions
Large diffs are not rendered by default.

dist/angular-leaflet-directive.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directives/bounds.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,35 @@ angular.module("leaflet-directive").directive('bounds', function ($log) {
77
require: 'leaflet',
88

99
link: function($scope, element, attrs, controller) {
10-
var map = controller.getMap();
11-
setupBounds(map);
10+
controller.getMap().then(function(map) {
11+
setupBounds(map);
1212

13-
function isBoundsValid(bounds) {
14-
return isDefined(bounds) && isDefined(bounds.southWest) &&
15-
isDefined(bounds.northEast) && isNumber(bounds.southWest.lat) &&
16-
isNumber(bounds.southWest.lng) && isNumber(bounds.northEast.lat) &&
17-
isNumber(bounds.northEast.lng);
18-
}
13+
function isBoundsValid(bounds) {
14+
return isDefined(bounds) && isDefined(bounds.southWest) &&
15+
isDefined(bounds.northEast) && isNumber(bounds.southWest.lat) &&
16+
isNumber(bounds.southWest.lng) && isNumber(bounds.northEast.lat) &&
17+
isNumber(bounds.northEast.lng);
18+
}
1919

20-
function setupBounds(map) {
21-
$scope.$watch('bounds', function(bounds) {
22-
if (!isDefined(bounds) || !isBoundsValid(bounds)) {
23-
$log.error('[AngularJS - Leaflet] Invalid bounds');
24-
return;
25-
}
20+
function setupBounds(map) {
21+
$scope.$watch('bounds', function(bounds) {
22+
if (!isDefined(bounds) || !isBoundsValid(bounds)) {
23+
$log.error('[AngularJS - Leaflet] Invalid bounds');
24+
return;
25+
}
2626

27-
var southWest = bounds.southWest;
28-
var northEast = bounds.northEast;
29-
var new_latlng_bounds = new L.LatLngBounds(
30-
new L.LatLng(southWest.lat, southWest.lng),
31-
new L.LatLng(northEast.lat, northEast.lng));
27+
var southWest = bounds.southWest;
28+
var northEast = bounds.northEast;
29+
var new_latlng_bounds = new L.LatLngBounds(
30+
new L.LatLng(southWest.lat, southWest.lng),
31+
new L.LatLng(northEast.lat, northEast.lng));
3232

33-
if (!map.getBounds().equals(new_latlng_bounds)) {
34-
map.fitBounds(new_latlng_bounds);
35-
}
36-
}, true);
37-
}
33+
if (!map.getBounds().equals(new_latlng_bounds)) {
34+
map.fitBounds(new_latlng_bounds);
35+
}
36+
}, true);
37+
}
38+
});
3839
}
3940
};
4041
});

src/directives/center.js

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,87 @@ angular.module("leaflet-directive").directive('center', function ($log, $parse)
88

99
link: function($scope, element, attrs, controller) {
1010
var defaults = parseMapDefaults($scope.defaults);
11-
var map = controller.getMap();
1211
var center = $scope.center;
1312

14-
setupCenter(map, center, defaults);
13+
controller.getMap().then(function(map) {
14+
setupCenter(map, center, defaults);
1515

16-
function updateBoundsInScope(map) {
17-
if (!$scope.bounds) {
18-
return;
19-
}
20-
21-
var bounds = map.getBounds();
22-
var sw_latlng = bounds.getSouthWest();
23-
var ne_latlng = bounds.getNorthEast();
24-
$scope.bounds = {
25-
southWest: {
26-
lat: sw_latlng.lat,
27-
lng: sw_latlng.lng
28-
},
29-
northEast: {
30-
lat: ne_latlng.lat,
31-
lng: ne_latlng.lng
32-
}
33-
};
34-
}
35-
36-
function updateCenter(map, center) {
37-
map.setView([center.lat, center.lng], center.zoom);
38-
updateBoundsInScope(map);
39-
}
40-
41-
function isValidCenter(center) {
42-
return isDefined(center) && isNumber(center.lat) && isNumber(center.lng) && isNumber(center.zoom);
43-
}
44-
45-
function setupCenter(map, center, defaults) {
46-
if (isDefined(center)) {
47-
if (center.autoDiscover === true) {
48-
map.locate({ setView: true, maxZoom: defaults.maxZoom });
16+
function updateBoundsInScope(map) {
17+
if (!$scope.bounds) {
18+
return;
4919
}
5020

51-
var centerModel = {
52-
lat: $parse("center.lat"),
53-
lng: $parse("center.lng"),
54-
zoom: $parse("center.zoom")
21+
var bounds = map.getBounds();
22+
var sw_latlng = bounds.getSouthWest();
23+
var ne_latlng = bounds.getNorthEast();
24+
$scope.bounds = {
25+
southWest: {
26+
lat: sw_latlng.lat,
27+
lng: sw_latlng.lng
28+
},
29+
northEast: {
30+
lat: ne_latlng.lat,
31+
lng: ne_latlng.lng
32+
}
5533
};
5634
}
5735

58-
var movingMap = false;
36+
function updateCenter(map, center) {
37+
map.setView([center.lat, center.lng], center.zoom);
38+
updateBoundsInScope(map);
39+
}
5940

60-
$scope.$watch("center", function(center) {
61-
if (!isValidCenter(center)) {
62-
$log.warn("[AngularJS - Leaflet] invalid 'center'");
63-
updateCenter(map, defaults.center);
64-
return;
65-
}
41+
function isValidCenter(center) {
42+
return isDefined(center) && isNumber(center.lat) && isNumber(center.lng) && isNumber(center.zoom);
43+
}
6644

67-
if (movingMap) {
68-
// Can't update. The map is moving.
69-
return;
45+
function setupCenter(map, center, defaults) {
46+
if (isDefined(center)) {
47+
if (center.autoDiscover === true) {
48+
map.locate({ setView: true, maxZoom: defaults.maxZoom });
49+
}
50+
51+
var centerModel = {
52+
lat: $parse("center.lat"),
53+
lng: $parse("center.lng"),
54+
zoom: $parse("center.zoom")
55+
};
7056
}
7157

72-
updateCenter(map, center);
73-
}, true);
58+
var movingMap = false;
7459

75-
map.on("movestart", function(/* event */) {
76-
movingMap = true;
77-
});
60+
$scope.$watch("center", function(center) {
61+
if (!isValidCenter(center)) {
62+
$log.warn("[AngularJS - Leaflet] invalid 'center'");
63+
updateCenter(map, defaults.center);
64+
return;
65+
}
7866

79-
map.on("moveend", function(/* event */) {
80-
movingMap = false;
81-
safeApply($scope, function(scope) {
82-
if (centerModel) {
83-
centerModel.lat.assign(scope, map.getCenter().lat);
84-
centerModel.lng.assign(scope, map.getCenter().lng);
85-
centerModel.zoom.assign(scope, map.getZoom());
67+
if (movingMap) {
68+
// Can't update. The map is moving.
69+
return;
8670
}
87-
scope.$emit("centerUpdated");
71+
72+
updateCenter(map, center);
73+
}, true);
74+
75+
map.on("movestart", function(/* event */) {
76+
movingMap = true;
8877
});
89-
});
90-
}
78+
79+
map.on("moveend", function(/* event */) {
80+
movingMap = false;
81+
safeApply($scope, function(scope) {
82+
if (centerModel) {
83+
centerModel.lat.assign(scope, map.getCenter().lat);
84+
centerModel.lng.assign(scope, map.getCenter().lng);
85+
centerModel.zoom.assign(scope, map.getZoom());
86+
}
87+
scope.$emit("centerUpdated");
88+
});
89+
});
90+
}
91+
});
9192
}
9293
};
9394
});

src/directives/geojson.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ angular.module("leaflet-directive").directive('geojson', function ($log, $rootSc
99
link: function($scope, element, attrs, controller) {
1010
var map = controller.getMap();
1111
var leafletGeoJSON;
12-
setupGeoJSON(map);
1312

14-
function setupGeoJSON(map, geojson, defaults) {
13+
controller.getMap().then(function(map) {
1514
$scope.$watch("geojson", function(geojson) {
1615
if (!isDefined(geojson)) {
1716
return;
@@ -53,7 +52,7 @@ angular.module("leaflet-directive").directive('geojson', function ($log, $rootSc
5352
}).addTo(map);
5453
}
5554
});
56-
}
55+
});
5756
}
5857
};
5958
});

src/directives/layers.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ angular.module("leaflet-directive").directive('layers', function ($log, leafletD
1212
},
1313
link: function($scope, element, attrs, controller) {
1414
var defaults = parseMapDefaults($scope.defaults);
15-
var map = controller.getMap();
1615
var layers = $scope.layers;
16+
17+
controller.getMap().then(function(map) {
18+
1719
setupLayers(map, layers, defaults);
1820

1921
function setupLayers(map, layers, defaults) {
@@ -250,6 +252,7 @@ angular.module("leaflet-directive").directive('layers', function ($log, leafletD
250252
return null;
251253
}
252254
}
255+
});
253256
}
254257
};
255258
});

src/directives/legend.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ angular.module("leaflet-directive").directive('legend', function ($log) {
77
require: 'leaflet',
88

99
link: function($scope, element, attrs, controller) {
10-
var map = controller.getMap();
11-
var legend = $scope.legend;
10+
controller.getMap().then(function(map) {
11+
var legend = $scope.legend;
1212

13-
setupLegend(map, legend);
14-
15-
function setupLegend(map, legend) {
1613
if (!isArray(legend.colors) || !isArray(legend.labels) || legend.colors.length !== legend.labels.length) {
17-
$log.warn("[AngularJS - Leaflet] legend.colors and legend.labels must be set.");
14+
$log.warn("[AngularJS - Leaflet] legend.colors and legend.labels must be set.");
1815
} else {
1916
var legendClass = legend.legendClass ? legend.legendClass : "legend";
2017
var position = legend.position || 'bottomright';
@@ -29,7 +26,7 @@ angular.module("leaflet-directive").directive('legend', function ($log) {
2926
};
3027
leafletLegend.addTo(map);
3128
}
32-
}
29+
});
3330
}
3431
};
3532
});

src/directives/map.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
angular.module("leaflet-directive", []).directive('leaflet', function ($log, leafletData) {
1+
angular.module("leaflet-directive", []).directive('leaflet', function ($log, $q, leafletData) {
22
return {
33
restrict: "E",
44
replace: true,
@@ -20,8 +20,9 @@ angular.module("leaflet-directive", []).directive('leaflet', function ($log, lea
2020
},
2121
template: '<div class="angular-leaflet-map" ng-transclude></div>',
2222
controller: function ($scope) {
23+
$scope.leafletMapDeferred = $q.defer();
2324
this.getMap = function () {
24-
return $scope.leafletMap;
25+
return $scope.leafletMapDeferred.promise;
2526
};
2627
},
2728

@@ -34,29 +35,21 @@ angular.module("leaflet-directive", []).directive('leaflet', function ($log, lea
3435
}
3536

3637
// Set width and height if they are defined
37-
if (attrs.width) {
38-
if (isNaN(attrs.width)) {
38+
if (isDefined(attrs.width)) {
39+
if (!isNumber(attrs.width)) {
3940
element.css('width', attrs.width);
4041
} else {
4142
element.css('width', attrs.width + 'px');
4243
}
4344
}
44-
if (attrs.height) {
45-
if (isNaN(attrs.height)) {
45+
if (isDefined(attrs.height)) {
46+
if (isNumber(attrs.height)) {
4647
element.css('height', attrs.height);
4748
} else {
4849
element.css('height', attrs.height + 'px');
4950
}
5051
}
5152

52-
// REVIEW
53-
// use of leafletDirectiveSetMap event is not encouraged. only use
54-
// it when there is no easy way to bind data to the directive
55-
$scope.$on('leafletDirectiveSetMap', function(event, message) {
56-
var meth = message.shift();
57-
map[meth].apply(map, message);
58-
});
59-
6053
// Create the Leaflet Map Object with the options
6154
var map = new L.Map(element[0], {
6255
maxZoom: defaults.maxZoom,
@@ -66,7 +59,7 @@ angular.module("leaflet-directive", []).directive('leaflet', function ($log, lea
6659
attributionControl: defaults.attributionControl
6760
});
6861

69-
$scope.leafletMap = map;
62+
$scope.leafletMapDeferred.resolve(map);
7063
leafletData.setMap(map);
7164
if (!isDefined(attrs.center)) {
7265
$log.warn("[AngularJS - Leaflet] 'center' is undefined in the current scope, did you forget to initialize it?");

src/directives/marker.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ angular.module("leaflet-directive").directive('marker', function ($log, $rootSco
88

99
link: function($scope, element, attrs, controller) {
1010
var defaults = parseMapDefaults($scope.defaults);
11-
var map = controller[0].getMap();
11+
var mapController = controller[0];
12+
13+
mapController.getMap().then(function(map) {
14+
1215
var marker = $scope.marker;
1316
var getLayers = function() {
1417
return [];
@@ -763,6 +766,7 @@ angular.module("leaflet-directive").directive('marker', function ($log, $rootSco
763766
map.addControl($scope.customControls[i]);
764767
}
765768
}
769+
});
766770
}
767771
};
768772
});

src/directives/markers.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ angular.module("leaflet-directive").directive('markers', function ($log, $rootSc
88

99
link: function($scope, element, attrs, controller) {
1010
var defaults = parseMapDefaults($scope.defaults);
11-
var map = controller[0].getMap();
11+
var mapController = controller[0];
12+
13+
mapController.getMap().then(function(map) {
14+
1215
var markers = $scope.markers;
1316
var getLayers = function() {
1417
return [];
1518
};
1619
if (isDefined(controller[1])) {
1720
getLayers = controller[1].getLayers;
1821
}
19-
$log.warn("ye", NaN, isNumber(NaN));
2022
// Default leaflet icon object used in all markers as a default
2123
var LeafletIcon = L.Icon.extend({
2224
options: {
@@ -797,6 +799,7 @@ $log.warn("ye", NaN, isNumber(NaN));
797799
map.addControl($scope.customControls[i]);
798800
}
799801
}
802+
});
800803
}
801804
};
802805
});

0 commit comments

Comments
 (0)