Skip to content

Commit 69d7989

Browse files
author
Chris Boden
committed
[Polylines] Init polylines directive
1 parent 7f7bd31 commit 69d7989

File tree

2 files changed

+129
-17
lines changed

2 files changed

+129
-17
lines changed

src/controllers/angulargmMapController.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
(function () {
88
angular.module('AngularGM').
99

10-
controller('angulargmMapController',
10+
controller('angulargmMapController',
1111
['$scope', '$element', 'angulargmUtils', 'angulargmDefaults',
1212
'angulargmContainer',
1313

@@ -27,7 +27,7 @@
2727
consts.precision = 3;
2828

2929

30-
/*
30+
/*
3131
* Construct a new controller for the gmMap directive.
3232
* @param {angular.Scope} $scope
3333
* @param {angular.element} $element
@@ -42,7 +42,7 @@
4242
mapDiv.attr('id', mapId);
4343

4444
var config = this._getConfig($scope, gMDefaults);
45-
45+
4646
// 'private' properties
4747
this._map = this._createMap(mapId, mapDiv, config, gMContainer, $scope);
4848
this._markers = {};
@@ -63,13 +63,13 @@
6363
return this._map.getCenter();
6464
},
6565
set: function(center) {
66-
if (hasNaN(center))
66+
if (hasNaN(center))
6767
throw 'center contains null or NaN';
6868
var changed = !latLngEqual(this.center, center);
6969
if (changed) {
7070
this._map.panTo(center);
7171
}
72-
}
72+
}
7373
},
7474

7575
'zoom': {
@@ -78,7 +78,7 @@
7878
return this._map.getZoom();
7979
},
8080
set: function(zoom) {
81-
if (!(zoom != null && !isNaN(zoom)))
81+
if (!(zoom != null && !isNaN(zoom)))
8282
throw 'zoom was null or NaN';
8383
var changed = this.zoom !== zoom;
8484
if (changed) {
@@ -95,7 +95,7 @@
9595
set: function(bounds) {
9696
var numbers = !hasNaN(bounds.getSouthWest()) &&
9797
!hasNaN(bounds.getNorthEast());
98-
if (!numbers)
98+
if (!numbers)
9999
throw 'bounds contains null or NaN';
100100

101101
var changed = !(boundsEqual(this.bounds, bounds));
@@ -152,20 +152,20 @@
152152
return map;
153153
};
154154

155-
155+
156156
// Set up listeners to update this.dragging
157157
this._initDragListeners = function() {
158158
var self = this;
159159
this.addMapListener('dragstart', function () {
160160
self.dragging = true;
161161
});
162-
162+
163163
this.addMapListener('idle', function () {
164164
self.dragging = false;
165165
});
166-
166+
167167
this.addMapListener('drag', function() {
168-
self.dragging = true;
168+
self.dragging = true;
169169
});
170170
};
171171

@@ -179,12 +179,12 @@
179179
var self = this;
180180
angular.forEach(scopeIds, function(scopeId) {
181181
self.forEachMarkerInScope(scopeId, function(marker, hash) {
182-
self.removeMarkerByHash(scopeId, hash);
182+
self.removeMarkerByHash(scopeId, hash);
183183
});
184184
});
185185
};
186186

187-
187+
188188
/**
189189
* Alias for google.maps.event.addListener(map, event, handler)
190190
* @param {string} event an event defined on google.maps.Map
@@ -204,7 +204,7 @@
204204
* @ignore
205205
*/
206206
this.addMapListenerOnce = function(event, handler) {
207-
google.maps.event.addListenerOnce(this._map,
207+
google.maps.event.addListenerOnce(this._map,
208208
event, handler);
209209
};
210210

@@ -269,15 +269,25 @@
269269
if (this.hasMarker(scopeId, position.lat(), position.lng())) {
270270
return false;
271271
}
272-
272+
273273
var hash = position.toUrlValue(this.precision);
274274
if (this._markers[scopeId] == null) {
275275
this._markers[scopeId] = {};
276276
}
277277
this._markers[scopeId][hash] = marker;
278278
marker.setMap(this._map);
279279
return true;
280-
};
280+
};
281+
282+
this.addPolyline = function(scopeId, polylineOptions) {
283+
var opts = {};
284+
angular.extend(opts, polylineOptions);
285+
286+
// check each lat/lng
287+
var polyline = new angulargmDefaults.polylineConstructor(opts);
288+
polyline.setMap(this._map);
289+
return true;
290+
}
281291

282292

283293
/**
@@ -311,7 +321,7 @@
311321
} else {
312322
return null;
313323
}
314-
};
324+
};
315325

316326

317327
/**

src/directives/gmPolylines.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict';
2+
3+
// lines
4+
// lat/lng per
5+
// Stroke opts: colour, opacity, weight
6+
7+
(function () {
8+
9+
angular.module('AngularGM').
10+
11+
directive('gmPolylines', ['$parse', '$compile', '$timeout', '$log', 'angulargmUtils',
12+
function ($parse, $compile, $timeout, $log, angulargmUtils) {
13+
/** aliases */
14+
var latLngEqual = angulargmUtils.latLngEqual;
15+
var objToLatLng = angulargmUtils.objToLatLng;
16+
var getEventHandlers = angulargmUtils.getEventHandlers;
17+
18+
function link(scope, element, attrs, controller) {
19+
// check attrs
20+
if (!('gmObjects' in attrs)) {
21+
throw 'gmObjects attribute required';
22+
} else if (!('gmGetCoords' in attrs)) {
23+
throw 'gmGetCoords attribute required';
24+
}
25+
26+
var handlers = getEventHandlers(attrs); // map events -> handlers
27+
28+
// fn for updating polylines from objects
29+
var updatePolylines = function(scope, objects) {
30+
console.log('updating lines?', objects);
31+
32+
var objectHash = {};
33+
34+
angular.forEach(objects, function(object, i) {
35+
var coords = scope.gmGetCoords({object: object});
36+
var lineLatLngs = [];
37+
var hash = '';
38+
39+
angular.forEach(coords, function(latlng, j) {
40+
var position = objToLatLng(latlng);
41+
if (null === position) {
42+
$log.warn('Unable to generate lat/lng from ', latlng);
43+
return;
44+
}
45+
46+
lineLatLngs.push(position);
47+
hash += position.toUrlValue(controller.precision);
48+
});
49+
50+
var polylineOptions = scope.gmGetPolylineOptions({object: object});
51+
objectHash[hash] = object;
52+
53+
// check if the polyline exists first
54+
{
55+
var options = {};
56+
angular.extend(options, polylineOptions, {path: lineLatLngs});
57+
58+
controller.addPolyline(scope.$id, options);
59+
}
60+
});
61+
}
62+
63+
// remove 'orphaned' polylines
64+
65+
scope.$watch('gmObjects().length', function(newValue, oldValue) {
66+
if (newValue != null && newValue !== oldValue) {
67+
updatePolylines(scope, scope.gmObjects());
68+
}
69+
});
70+
71+
scope.$watch('gmObjects', function(newValue, oldValue) {
72+
if (newValue != null && newValue !== oldValue) {
73+
updatePolylines(scope, scope.gmObjects());
74+
}
75+
});
76+
77+
// watch gmEvents
78+
79+
scope.$on('gmPolylinesRedraw', function(event, objectsName) {
80+
if (objectsName == null || objectsName === attrs.gmObjects) {
81+
updatePolylines(scope);
82+
updatePolylines(scope, scope.gmObjects());
83+
}
84+
});
85+
86+
$timeout(angular.bind(null, updatePolylines, scope, scope.gmObjects()));
87+
}
88+
89+
return {
90+
restrict: 'AE',
91+
priority: 100,
92+
scope: {
93+
gmObjects: '&',
94+
gmGetCoords: '&',
95+
gmGetPolylineOptions: '&',
96+
gmEvents: '&'
97+
},
98+
require: '^gmMap',
99+
link: link
100+
};
101+
}]);
102+
})();

0 commit comments

Comments
 (0)