polygon.js 6.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
 * Developed by The Di Lab
 * www.the-di-lab.com
 * 22.06.2010
 */
function PolygonCreator(map) {
	this.map = map;
	this.pen = new Pen(this.map);
	var thisOjb = this;
10
11
12
	proj4.defs("EPSG:31467", "+proj=tmerc +lat_0=0 +lon_0=9 +k=1 +x_0=3500000 +y_0=0"
			+ " +ellps=bessel +datum=potsdam +units=m +no_defs"); // http://spatialreference.org/ref/epsg/31467/proj4js/
	this.event = google.maps.event.addListener(thisOjb.map, 'click', function(event) {
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
		thisOjb.pen.draw(event.latLng);
	});
	this.destroy = function() {
		this.pen.deleteMis();
		$('#dataPanel').empty();
		if (null != this.pen.polygon) {
			this.pen.polygon.remove();
		}
		google.maps.event.removeListener(this.event);
	}
}

function Pen(map) {
	this.map = map;
	this.listOfDots = new Array();
	this.polyline = null;
	this.polygon = null;
	this.currentDot = null;
31
	this.i = 0;
32
33
34
35
	this.draw = function(latLng) {
		if (null != this.polygon) {
			alert('Click Reset to draw another');
		} else {
36
			if (this.currentDot != null && this.listOfDots.length > 1 && this.currentDot == this.listOfDots[0]) {
37
				this.drawPolygon(this.listOfDots);
38
				this.refreshInfo();
39
40
41
42
			} else {
				if (null != this.polyline) {
					this.polyline.remove();
				}
43
44
				var dot = new Dot(latLng, this.i, this.map, this);
				this.i += 1;
45
				this.listOfDots.push(dot);
46
				this.refreshInfo();
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
				if (this.listOfDots.length > 1) {
					this.polyline = new Line(this.listOfDots, this.map);
				}
			}
		}
	}
	this.drawPolygon = function(listOfDots, color, des, id) {
		this.polygon = new Polygon(listOfDots, this.map, this, color, des, id);
	}
	this.deleteMis = function() {
		$.each(this.listOfDots, function(index, value) {
			value.remove();
		});
		this.listOfDots.length = 0;
		if (null != this.polyline) {
			this.polyline.remove();
			this.polyline = null;
		}
	}
	this.setCurrentDot = function(dot) {
		this.currentDot = dot;
	}
	this.getListOfDots = function() {
		return this.listOfDots;
	}
72
	this.getWGS84Data = function() {
73
74
75
		var data = "";
		for (var i = 0; i < this.getListOfDots().length; i++) {
			data += this.getListOfDots()[i].latLng + "<br/>";
76
		}
77
78
79
80
81
82
83
84
		return data;
	}
	this.refreshInfo = function() {
		$('#dataPanel').empty();
		$('#dataPanel').append("WGS84 Coordinates<br/>");
		$('#dataPanel').append(this.getWGS84Data() + "<br/>\n");
		$('#dataPanel').append("GSK3 Coordinates<br/>");
		$('#dataPanel').append(this.getGSK3DataAndArea());
85
86
87
	}

	this.getGSK3DataAndArea = function(referenceSystem) {
88
89
90
91
92
93
94
95
96
		var data = "";
		var fromProjection = proj4('EPSG:4326');
		var toProjection = proj4('EPSG:31467');
		var xs = [];
		var ys = [];
		var listOfDots = this.getListOfDots();
		var n = listOfDots.length;
		for (var i = 0; i < n; i++) {
			var latLng = listOfDots[i].latLng;
97
			var gk3_coords = proj4(fromProjection, toProjection, [ latLng.lng(), latLng.lat() ]);
98
99
100
101
102
			xs.push(gk3_coords[0] - 3500000);
			ys.push(gk3_coords[1] - 5000000);
			data += "(" + (gk3_coords.toString() + ")<br/>");
		}
		if (n > 2) {
103
			data += "<br/>\nArea : " + (Math.round(polygonArea(xs, ys) / 1000) / 10).toString() + " ha<br/>";
104
		}
105
		return data;
106
	}
107

108
109
110
111
112
113
114
115
	this.updateDot = function(marker) {
		var old_dot = this.listOfDots[marker.zIndex];
		old_dot.setLatLng(marker.getPosition());
		if (null != this.polygon) {
			(this.polygon.remove());
			this.polygon = null;
			this.drawPolygon(this.listOfDots);
		}
duminil's avatar
duminil committed
116
117
		this.polyline.remove();
		this.polyline = new Line(this.listOfDots, this.map);
118
		this.refreshInfo();
119
120
121
	}
}

122
function Dot(latLng, i, map, pen) {
123
124
125
126
	this.latLng = latLng;
	this.parent = pen;
	this.markerObj = new google.maps.Marker({
		position : this.latLng,
127
128
129
		draggable : true,
		map : map,
		zIndex : i
130
131
132
133
134
135
	});
	this.addListener = function() {
		var parent = this.parent;
		var thisMarker = this.markerObj;
		var thisDot = this;
		google.maps.event.addListener(thisMarker, 'click', function() {
136
			// console.log("CLICKED");
137
138
139
140
141
			parent.setCurrentDot(thisDot);
			parent.draw(thisMarker.getPosition());
		});
	}
	this.addListener();
142
143
144
145
146
147
148
149
150
151
	this.addListener = function() {
		var parent = this.parent;
		var thisMarker = this.markerObj;
		var thisDot = this;
		google.maps.event.addListener(thisMarker, 'drag', function() {
			// console.log("DRAGGED");
			parent.updateDot(thisMarker);
		});
	}
	this.addListener();
152
153
154
155
156
157
158
159
160
	this.getLatLng = function() {
		return this.latLng;
	}
	this.getMarkerObj = function() {
		return this.markerObj;
	}
	this.remove = function() {
		this.markerObj.setMap(null);
	}
161
162
163
	this.setLatLng = function(latLng) {
		this.latLng = latLng;
	}
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
}

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);
	}
}

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.35,
		map : this.map
	});
	this.remove = function() {
		this.polygonObj.setMap(null);
	}
	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.addListener = function() {
		var thisPolygon = this.polygonObj;
225
226
227
		google.maps.event.addListener(thisPolygon, 'rightclick', function(event) {
			return false;
		});
228
229
	}
	this.addListener();
230
231
232
233
234
235
236
237
238
239
240
241
}

function polygonArea(X, Y, numPoints) {
	area = 0; // Accumulates area in the loop
	j = X.length - 1; // The last vertex is the 'previous' one to the first

	for (i = 0; i < X.length; i++) {
		area = area + (X[j] + X[i]) * (Y[j] - Y[i]);
		j = i; // j is previous vertex to i
	}
	return Math.abs(area / 2);
}