Skip to content

Commit 9bd0b24

Browse files
author
Andrew Sullivan
committed
Add on-zoom and on-reposition callbacks
Using scope['someCallbackMethodName']() seems very kludgy, but since we're purposefully not using isolate scope, we can't pass methods directly into our directive scope, so we're using the "callback" attrs as essentially keys to the methods on the parent scope.
1 parent e638438 commit 9bd0b24

4 files changed

Lines changed: 44 additions & 2 deletions

File tree

dist/angular-mapbox.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ angular.module('angularMapbox').directive('mapbox', function($compile, $q) {
5656
var group = new L.featureGroup(scope.markers);
5757
scope.map.fitBounds(group.getBounds());
5858
};
59+
60+
if(attrs.onReposition) {
61+
scope.map.on('dragend', function() {
62+
scope[attrs.onReposition](scope.map.getBounds());
63+
});
64+
}
65+
66+
if(attrs.onZoom) {
67+
scope.map.on('zoomend', function() {
68+
scope[attrs.onZoom](scope.map.getBounds());
69+
});
70+
}
5971
},
6072
template: '<div class="angular-mapbox-map" ng-transclude></div>',
6173
controller: function($scope) {

examples/example-app.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,14 @@ angularMapboxExample.controller('demoController', function($scope) {
6969
}
7070
]
7171
};
72+
73+
$scope.mapMovedCallback = function(bounds) {
74+
console.log('You repositioned the map to:');
75+
console.log(bounds);
76+
};
77+
78+
$scope.mapZoomedCallback = function(bounds) {
79+
console.log('You zoomed the map to:');
80+
console.log(bounds);
81+
};
7282
});

examples/index.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
<link rel="stylesheet" href="../lib/MarkerCluster.Default.css" />
99
</head>
1010
<body ng-app="angular-mapbox-example" ng-controller="demoController">
11-
<mapbox map-id="licyeus.i9pkgkg8" lat="47.643569" lng="-122.329453">
12-
<marker ng-repeat="market in farmersMarkets | filter: filterTerm" lat="{{market.coords.lat}}" lng="{{market.coords.lng}}" color="teal" icon="grocery">
11+
<mapbox map-id="licyeus.i9pkgkg8"
12+
lat="47.643569"
13+
lng="-122.329453"
14+
on-reposition="mapMovedCallback"
15+
on-zoom="mapZoomedCallback">
16+
<marker ng-repeat="market in farmersMarkets | filter: filterTerm"
17+
lat="{{market.coords.lat}}"
18+
lng="{{market.coords.lng}}"
19+
color="teal"
20+
icon="grocery">
1321
<h1>{{market.name}}</h1>
1422
<p>{{market.times}}</p>
1523
</marker>

src/directives/mapbox.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ angular.module('angularMapbox').directive('mapbox', function($compile, $q) {
3030
var group = new L.featureGroup(scope.markers);
3131
scope.map.fitBounds(group.getBounds());
3232
};
33+
34+
if(attrs.onReposition) {
35+
scope.map.on('dragend', function() {
36+
scope[attrs.onReposition](scope.map.getBounds());
37+
});
38+
}
39+
40+
if(attrs.onZoom) {
41+
scope.map.on('zoomend', function() {
42+
scope[attrs.onZoom](scope.map.getBounds());
43+
});
44+
}
3345
},
3446
template: '<div class="angular-mapbox-map" ng-transclude></div>',
3547
controller: function($scope) {

0 commit comments

Comments
 (0)