forked from jars/polysnapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolysnapper.js
275 lines (208 loc) · 9.3 KB
/
polysnapper.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
/*
_
| |
_ __ ___ | |_ _ ___ _ __ __ _ _ __ _ __ ___ _ __
| '_ \ / _ \| | | | / __| '_ \ / _` | '_ \| '_ \ / _ \ '__|
| |_) | (_) | | |_| \__ \ | | | (_| | |_) | |_) | __/ |
| .__/ \___/|_|\__, |___/_| |_|\__,_| .__/| .__/ \___|_|
| | __/ | | | | |
|_| |___/ |_| |_|
@jordanarseno - MIT LICENSE
*/
function PolySnapper(opts){
function extend(obj) {
Array.prototype.slice.call(arguments, 1).forEach(function(source) {
if (source) {
for (var prop in source) {
if (source[prop].constructor === Object) {
if (!obj[prop] || obj[prop].constructor === Object) {
obj[prop] = obj[prop] || {};
extend(obj[prop], source[prop]);
} else {
obj[prop] = source[prop];
}
} else {
obj[prop] = source[prop];
}
}
}
});
return obj;
}
function defined(obj, key){
return typeof obj[key] !== 'undefined'
}
var that = this;
this.keyDownListener = null;
this.keyUpListener = null;
this.drawing = false;
this.currentpoly = null;
this.polys = ( defined(opts, 'polygons') )? opts.polygons : [];
var _map = ( defined(opts, 'map') )? opts.map : null;
var _marker = ( defined(opts, 'marker') )? opts.marker : new google.maps.Marker();
var _thresh = ( defined(opts, 'threshold') )? opts.threshold : 20;
var _key = ( defined(opts, 'key') )? opts.key : 'shift';
var _keyReq = ( defined(opts, 'keyRequired') )? opts.keyRequired : false;
var _onEnabled = ( defined(opts, 'onEnabled') )? opts.onEnabled : function(){};
var _onDisabled = ( defined(opts, 'onDisabled') )? opts.onDisabled : function(){};
var _onChange = ( defined(opts, 'onChange') )? opts.onChange : function(){};
var _polystyle = ( defined(opts, 'polystyle') )? (JSON.parse(JSON.stringify(opts.polystyle))) : {};
var _hidePOI = ( defined(opts, 'hidePOI') )? opts.hidePOI : false;
var _keyDown = false;
if( !_map ){
console.log("We need to know the map");
return;
}
var _mapDiv = document.getElementById( _map.getDiv().getAttribute('id') );
if( _hidePOI ){
_map.poi = function(state){
var styles = [
{
"featureType": "transit",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "poi",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "landscape",
"stylers": [
{ "visibility": "off" }
]
}
];
this.set("styles", (state)? {} : styles );
}
}
if( _keyReq ){
var keymap = {
'shift': 16,
'ctrl': 17
}
var which = keymap[_key];
this.keyDownListener = window.addEventListener("keydown", function(e){
_keyDown = (e.which == which);
});
this.keyUpListener = window.addEventListener("keyup", function(e){
_keyDown = (e.which == which)? false : true;
});
}
return {
polygon: function(){
return that.currentpoly;
},
enabled: function(){
return that.drawing;
},
enable: function(){
that.drawing = true;
if( _hidePOI ) _map.poi(false);
var vertexMarker = _marker;
var snapable_polys = that.polys.filter( function(p){ return ( typeof p.snapable !== 'undefined' && p.snapable ) } );
var snapable_points = snapable_polys.map( function(p){ return p.getPath().getArray() } ).reduce(function(a,b){ return a.concat(b) }, []);
var last_closeby = null;
//the official Drawing Manager will not work!
_map.setOptions({draggableCursor:'crosshair'});
that.currentpoly = new google.maps.Polygon(
extend( _polystyle, {editable: true, map: _map})
);
that.currentpoly.addListener('rightclick', function(e){
if (e.vertex != null && this.getPath().getLength() > 3) {
this.getPath().removeAt(e.vertex);
_onChange();
}
});
//you can delete vertices in the current polygon by right clicking them
_map.addListener("click", function(e){
// Because path is an MVCArray, we can simply append a new coordinate
// and it will automatically appear.
var ll = (last_closeby && (!_keyReq || _keyReq && _keyDown) )? last_closeby : e.latLng;
that.currentpoly.getPath().push(ll);
_onChange();
});
/*listening to set_at event, and calling the setAt() method inside
will cause a Maximum call stack size exceeded...
google.maps.event.addListener(currentpoly.getPath(), "set_at", function(idx){
if(last_closeby) currentpoly.getPath().setAt(idx, last_closeby);
});
Instead, we can addListenerOnce, and make sure to re-attach the listner AFTER setAt
*/
(function setAtRecurse(){
google.maps.event.addListenerOnce(currentpoly.getPath(), "set_at", function(idx){
if(last_closeby && (!_keyReq || _keyReq && _keyDown)) that.currentpoly.getPath().setAt(idx, last_closeby);
setAtRecurse();
_onChange();
});
}());
//Same comments go for insert_at ...
(function insertAtRecurse(){
google.maps.event.addListenerOnce(currentpoly.getPath(), "insert_at", function(idx){
if(last_closeby && (!_keyReq || _keyReq && _keyDown)) that.currentpoly.getPath().setAt(idx, last_closeby);
insertAtRecurse();
_onChange();
});
}());
/*
we cannot listen to move events on the gmap object.. because when we
drag existing points, or new ones, the mouse move events are suspended
instead, we must attach mousemove to the mapcanvas (jquery), and then
convert x,y coordinates in the map canvas to lat lng points.
*/
_mapDiv.onmousemove = function(e){
bounds = _map.getBounds();
neLatlng = bounds.getNorthEast();
swLatlng = bounds.getSouthWest();
startLat = neLatlng.lat();
endLng = neLatlng.lng();
endLat = swLatlng.lat();
startLng = swLatlng.lng();
lat = startLat + (( e.offsetY/ this.offsetHeight ) * (endLat - startLat));
lng = startLng + (( e.offsetX/ this.offsetWidth ) * (endLng - startLng));
var ll = new google.maps.LatLng(lat, lng);
//find any of the existing polygon points are close to the mousepointer
var closeby = snapable_points.filter ( function(p){
return ( google.maps.geometry.spherical.computeDistanceBetween(ll, p) ) < _thresh
})[0] || null;
/* we could just use:
if(closeby){
vertexMarker.setOptions({
position: closeby,
map: map
});
}
else vertexMarker.setMap(null);
However, it causes the marker to flicker because we are constantly calling
setOptions every mousemove. We will instead, save the last position of closeby,
and only set it again if it has changed...
*/
if(closeby && closeby != last_closeby){
last_closeby = closeby;
vertexMarker.setPosition(closeby);
vertexMarker.setMap(_map);
}
else if(!closeby) {
last_closeby = null;
vertexMarker.setMap(null);
}
};
//now execute the callback
_onEnabled();
},
disable: function(){
if(_hidePOI) _map.poi(true);
that.drawing = false;
_map.setOptions({draggableCursor:null});
that.currentpoly.setMap(null);
_mapDiv.onmousemove = null;
if(_keyReq){
window.removeEventListener("keydown", this.keyDownListener);
window.removeEventListener("keyup", this.keyUpListener);
}
//annnd the callback
_onDisabled();
}
}
}