-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
302 lines (252 loc) · 10 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
;(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
window.GMapPolygon = factory()
}(this, function () { 'use strict';
class GMapPolygon {
constructor(options) {
if (!options.map) {
return console.error('options.map is required');
}
this.options = options;
this.coords = [];
this.polyline = null;
this.polygon = null;
this.handles = null;
this.handlePolyline = null;
this.markers = null;
this.startingPoint = null;
this.polygonIsComplete = false;
this.listeners = [];
}
init(path) {
this.options.map.setOptions({ draggableCursor: 'crosshair' });
if (path && path.length) {
this.coords = path;
this.setEditMode();
} else {
this.addListener(this.options.map, 'click', this.onShapeClicked, 'map');
this.listeners.push( // add to the list even if listenOnce in case of multiple initializations
google.maps.event.addListenerOnce(this.options.map, 'click', this.onMapFirstClick.bind(this))
);
}
return this;
}
addListener(instance, eventType, cb, context) {
const callback = context !== undefined ? cb.bind(this, context) : cb.bind(this);
this.listeners.push(
google.maps.event.addListener(instance, eventType, callback)
);
}
destroy() {
// removes all the gmap shapes
this.destroyShape([this.handlePolyline, this.startingPoint, this.polyline, this.polygon]);
this.destroyShape(this.markers);
this.destroyShape(this.handles);
// remove all the listeners
this.destroyMapListeners();
return this;
}
destroyShape(shapes) {
if (!Array.isArray(shapes)) {
shapes = [shapes];
}
shapes.forEach(function (shape) {
if (shape) {
google.maps.event.clearInstanceListeners(shape);
shape.setMap(null);
shape = null;
}
});
}
destroyMapListeners() {
this.listeners.forEach(listener => google.maps.event.removeListener(listener));
this.listeners = [];
}
getMarker(latLng) {
const iconOptions = extend({
path: google.maps.SymbolPath.CIRCLE
}, this.options.styles.point);
return new google.maps.Marker({
position: latLng,
map: this.options.map,
icon: iconOptions
});
}
drawStartingPoint(latLng) {
this.startingPoint = this.getMarker(latLng);
this.addListener(this.startingPoint, 'click', this.onStartingPointClicked);
}
drawPolyline(path) {
path = path || this.coords;
if (!path.length) {
//not enough coords
return;
}
const params = extend({
path: path,
map: this.options.map
}, this.options.styles.line);
if (!this.polyline) {
// The Polyline has not yet been created, so let's do it and bind the events
this.polyline = new google.maps.Polyline(params);
this.addListener(this.polyline, 'click', this.onShapeClicked, 'polyline');
if (this.startingPoint) {
this.startingPoint.setOptions({zIndex: 200});
}
} else {
this.polyline.setOptions(params);
}
}
drawPolygon() {
const style = this.polygonIsComplete ? this.options.styles.polygonHighlight : this.options.styles.polygonMask,
params = extend({
map: this.options.map,
path: this.coords
}, style);
if (!this.polygon) {
// The polygon has not yet been created, so let's do it and bind the events
this.polygon = new google.maps.Polygon(params);
this.addListener(this.polygon, 'mousemove', this.onMouseMove);
this.addListener(this.polygon, 'click', this.onShapeClicked, 'polygon');
if (this.startingPoint) {
this.startingPoint.setOptions({zIndex: 200});
}
} else {
this.polygon.setOptions(params);
}
}
updateHandles() {
const coords = this.coords,
l = coords.length;
this.handles.forEach(function (handle, i) {
const nextIndex = (i + 1) % l,
distance = google.maps.geometry.spherical.computeDistanceBetween(coords[i], coords[nextIndex]),
latLng = distance > 7 ? google.maps.geometry.spherical.interpolate(coords[i], coords[nextIndex], 0.5) : coords[i],
zIndex = distance > 7 ? 200 : 0;
handle.setOptions({
position: latLng,
zIndex: zIndex
});
});
}
drawHandles() {
const self = this,
iconOptions = extend({
path: google.maps.SymbolPath.CIRCLE
}, this.options.styles.handle);
this.handles = this.coords.map(function (c, i) {
const marker = new google.maps.Marker({
map: self.options.map,
icon: iconOptions,
draggable: true
});
self.addListener(marker, 'drag', self.onHandleDragged, i);
self.addListener(marker, 'dragend', self.onHandleDragEnded, i);
return marker;
});
this.updateHandles();
}
addCoord(latLng) {
this.coords.push(latLng);
this.drawPolyline();
if (this.coords.length > 2) {
this.drawPolygon();
}
}
insertCoordAt(index, latLng) {
this.coords.splice(index, 0, latLng);
}
setPolygonComplete() {
this.polygonIsComplete = true;
// remove the lines & starting point
this.destroyShape([this.polyline, this.startingPoint]);
if (this.options.polygonCallback) { this.options.polygonCallback(this.coords); }
this.setEditMode();
}
setEditMode() {
this.polygonIsComplete = true;
this.destroyMapListeners();
this.destroyShape([this.polygon, this.handlePolyline]);
this.destroyShape(this.handles);
this.destroyShape(this.markers);
// draw the polygon before the markers so that is lays underneath
this.drawPolygon();
// kill all the listeners
const self = this;
this.markers = this.coords.map(function (c, i) {
const marker = self.getMarker(c);
marker.setOptions({
draggable: true,
zIndex: 200
});
self.addListener(marker, 'drag', self.onMarkerDragged);
return marker;
});
this.destroyShape(this.polyline);
this.drawHandles();
}
/**
* EVENT FLOW
*/
onMapFirstClick(event) {
this.drawStartingPoint(event.latLng);
this.addListener(this.options.map, 'mousemove', this.onMouseMove);
if (this.options.markerPlacedCallback) { this.options.markerPlacedCallback(); }
}
onMarkerDragged() {
this.coords = this.markers.map(m => m.getPosition());
this.drawPolygon();
this.updateHandles();
// don't forget to fire the polygon callback to notify of the new position
if (this.options.polygonCallback) { this.options.polygonCallback(this.coords); }
}
onHandleDragged(i, event) {
// draw a line between a & b which
const n = (i + 1) % this.coords.length,
path = [this.coords[i], event.latLng, this.coords[n]],
params = extend({
path: path,
map: this.options.map
}, this.options.styles.handleLine);
if (!this.handlePolyline) {
// The Polyline has not yet been created, so let's do it and bind the events
this.handlePolyline = new google.maps.Polyline(params);
} else {
this.handlePolyline.setOptions(params);
}
}
onHandleDragEnded(i, event) {
this.insertCoordAt(i + 1, event.latLng);
this.setEditMode();
// don't forget to fire the polygon callback to notify of the new position
if (this.options.polygonCallback) { this.options.polygonCallback(this.coords); }
}
onMouseMove(event) {
if (this.polygonIsComplete) { return; }
const path = this.coords.slice(0);
path.push(event.latLng);
this.drawPolyline(path);
}
onShapeClicked(shape, event) {
this.addCoord(event.latLng);
}
onStartingPointClicked() {
if (this.coords.length < 3) {
return;
}
this.setPolygonComplete();
}
};
function extend(out) {
out = out || {};
for (var i = 1; i < arguments.length; i++) {
if (!arguments[i]) { continue; }
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) { out[key] = arguments[i][key]; }
}
}
return out;
};
return GMapPolygon;
}));