polygon.js
6.79 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Polygon creator class
* Developed by The Di Lab
* www.the-di-lab.com
* 22.06.2010
*/
function PolygonCreator(map) {
//create pen to draw on map
this.map = map;
this.pen = new Pen(this.map);
this.latLng = '';
var thisOjb = this;
this.event = google.maps.event.addListener(thisOjb.map, 'click', function (event) {
thisOjb.latLng = event.latLng;
thisOjb.pen.draw(thisOjb.latLng);
});
this.getDot = function () {
return (this.latLng+"").replace(/[()]/g,"");
}
this.getData = function () {
return this.pen.getData() != null ? (this.pen.getData()+"").replace(/\)\(/g,";").replace(/[()]/g,"") : '';
}
this.showData = function () {
return this.pen.getData();
}
this.showColor = function () {
return this.pen.getColor();
}
//destroy the pen
this.destroy = function () {
this.pen.deleteMis();
if (null != this.pen.polygon) {
this.pen.polygon.remove();
}
google.maps.event.removeListener(this.event);
}
this.drawPolygon = function (latLngList) {
$.each(latLngList, function (index, value) {
thisOjb.pen.draw(value);
});
this.pen.drawPloygon(this.pen.listOfDots);
}
this.getPolygon = function () {
return this.pen.polygon.getPolygonObj();
}
}
/*
* pen class
*/
function Pen(map) {
this.map = map;
this.listOfDots = new Array();
this.polyline = null;
this.polygon = null;
this.currentDot = null;
//draw function
this.draw = function (latLng) {
if (null != this.polygon) {
//alert('已有一个图形区域。');
} else {
if (this.currentDot != null && this.listOfDots.length > 1 && this.currentDot == this.listOfDots[0]) {
this.drawPloygon(this.listOfDots);
} else {
//remove previous line
if (null != this.polyline) {
this.polyline.remove();
}
//draw Dot
var dot = new Dot(latLng, this.map, this);
this.listOfDots.push(dot);
//draw line
if (this.listOfDots.length > 1) {
this.polyline = new Line(this.listOfDots, this.map);
}
}
}
}
//draw ploygon
this.drawPloygon = function (listOfDots, color, des, id) {
this.polygon = new Polygon(listOfDots, this.map, this, color, des, id);
this.deleteMis();
}
//delete all dots and polylines
this.deleteMis = function () {
//delete dots
$.each(this.listOfDots, function (index, value) {
value.remove();
});
this.listOfDots.length = 0;
//delete lines
if (null != this.polyline) {
this.polyline.remove();
this.polyline = null;
}
}
//cancel
this.cancel = function () {
if (null != this.polygon) {
(this.polygon.remove());
}
this.polygon = null;
this.deleteMis();
}
//setter
this.setCurrentDot = function (dot) {
this.currentDot = dot;
}
//getter
this.getListOfDots = function () {
return this.listOfDots;
}
//get plots data
this.getData = function () {
if (this.polygon != null) {
var data = "";
var paths = this.polygon.getPlots();
//get paths
paths.getAt(0).forEach(function (value, index) {
data += (value.toString());
});
return data;
} else {
return null;
}
}
//get color
this.getColor = function () {
if (this.polygon != null) {
var color = this.polygon.getColor();
return color;
} else {
return null;
}
}
}
/*
* Child of Pen class
* dot class
*/
function Dot(latLng, map, pen) {
//property
this.latLng = latLng;
this.parent = pen;
this.markerObj = new google.maps.Marker({
position : this.latLng,
map : map
});
//closure
this.addListener = function () {
var parent = this.parent;
var thisMarker = this.markerObj;
var thisDot = this;
google.maps.event.addListener(thisMarker, 'click', function () {
parent.setCurrentDot(thisDot);
parent.draw(thisMarker.getPosition());
});
}
this.addListener();
//getter
this.getLatLng = function () {
return this.latLng;
}
this.getMarkerObj = function () {
return this.markerObj;
}
this.remove = function () {
this.markerObj.setMap(null);
}
}
/*
* Child of Pen class
* Line class
*/
function Line(listOfDots, map) {
this.listOfDots = listOfDots;
this.map = map;
this.coords = new Array();
this.polylineObj = null;
if (this.listOfDots.length > 1) {
var thisObj = this;
$.each(this.listOfDots, function (index, value) {
thisObj.coords.push(value.getLatLng());
});
this.polylineObj = new google.maps.Polyline({
path : this.coords,
strokeColor : "#FF0000",
strokeOpacity : 1.0,
strokeWeight : 2,
map : this.map
});
}
this.remove = function () {
this.polylineObj.setMap(null);
}
}
/*
* Child of Pen class
* polygon class
*/
function Polygon(listOfDots, map, pen, color) {
this.listOfDots = listOfDots;
this.map = map;
this.coords = new Array();
this.parent = pen;
this.des = 'Hello';
var thisObj = this;
$.each(this.listOfDots, function (index, value) {
thisObj.coords.push(value.getLatLng());
});
this.polygonObj = new google.maps.Polygon({
paths : this.coords,
strokeColor : "#FF0000",
strokeOpacity : 0.8,
strokeWeight : 2,
fillColor : "#FF0000",
fillOpacity : 0.05,
map : this.map
});
this.remove = function () {
this.info.remove();
this.polygonObj.setMap(null);
}
//getter
this.getContent = function () {
return this.des;
}
this.getPolygonObj = function () {
return this.polygonObj;
}
this.getListOfDots = function () {
return this.listOfDots;
}
this.getPlots = function () {
return this.polygonObj.getPaths();
}
this.getColor = function () {
return this.getPolygonObj().fillColor;
}
//setter
this.setColor = function (color) {
return this.getPolygonObj().setOptions({
fillColor : color,
strokeColor : color,
strokeWeight : 2
});
}
this.info = new Info(this, this.map);
//closure
this.addListener = function () {
var info = this.info;
var thisPolygon = this.polygonObj;
google.maps.event.addListener(thisPolygon, 'rightclick', function (event) {
info.show(event.latLng);
});
}
//this.addListener();
}
/*
* Child of Polygon class
* Info Class
*/
function Info(polygon, map) {
this.parent = polygon;
this.map = map;
this.color = document.createElement('input');
this.button = document.createElement('input');
$(this.button).attr('type', 'button');
$(this.button).val("Change Color");
var thisOjb = this;
//change color action
this.changeColor = function () {
thisOjb.parent.setColor($(thisOjb.color).val());
}
//get content
this.getContent = function () {
var content = document.createElement('div');
$(this.color).val(this.parent.getColor());
$(this.button).click(function () {
thisObj.changeColor();
});
$(content).append(this.color);
$(content).append(this.button);
return content;
}
thisObj = this;
this.infoWidObj = new google.maps.InfoWindow({
content : thisObj.getContent()
});
this.show = function (latLng) {
this.infoWidObj.setPosition(latLng);
this.infoWidObj.open(this.map);
}
this.remove = function () {
this.infoWidObj.close();
}
}