forked from GlidingWeb/IGCWebView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapcontrol.js
More file actions
283 lines (251 loc) · 8.78 KB
/
mapcontrol.js
File metadata and controls
283 lines (251 loc) · 8.78 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
// Wrapper for the leaflet.js map control with methods
// to manage the map layers.
function createMapControl(elementName) {
'use strict';
// Private methods for drawing turn point sectors and start / finish lines
function getBearing(pt1, pt2) {
// Get bearing from pt1 to pt2 in degrees
// Formula from: http://www.movable-type.co.uk/scripts/latlong.html
// Start by converting to radians.
var degToRad = Math.PI / 180.0;
var lat1 = pt1['lat'] * degToRad;
var lon1 = pt1['lng'] * degToRad;
var lat2 = pt2['lat'] * degToRad;
var lon2 = pt2['lng'] * degToRad;
var y = Math.sin(lon2 - lon1) * Math.cos(lat2);
var x = Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1);
var bearing = Math.atan2(y, x) / degToRad;
bearing = (bearing + 360.0) % 360.0;
return bearing;
}
function getLine(pt1, pt2, linerad, drawOptions) {
//returns line through pt1, at right angles to line between pt1 and pt2, length linerad.
//Use Pythogoras- accurate enough on this scale
var latdiff = pt2['lat'] - pt1['lat'];
var northmean = (pt1['lat'] + pt2['lat']) * Math.PI / 360;
var startrads = pt1['lat'] * Math.PI / 180;
var longdiff = (pt1['lng'] - pt2['lng']) * Math.cos(northmean);
var hypotenuse = Math.sqrt(latdiff * latdiff + longdiff * longdiff);
//assume earth is a sphere circumference 40030 Km
var latdelta = linerad * longdiff / hypotenuse / 111.1949269;
var longdelta = linerad * latdiff / hypotenuse / 111.1949269 / Math.cos(startrads);
var linestart = L.latLng(pt1['lat'] - latdelta, pt1['lng'] - longdelta);
var lineend = L.latLng(pt1['lat'] + latdelta, longdelta + pt1['lng']);
var polylinePoints = [linestart, lineend];
return L.polyline(polylinePoints, drawOptions);
}
function getTpSector(centrept, pt1, pt2, sectorRadius, sectorAngle, drawOptions) {
var headingIn = getBearing(pt1, centrept);
var bearingOut = getBearing(pt2, centrept);
var bisector = headingIn + (bearingOut - headingIn) / 2;
if (Math.abs(bearingOut - headingIn) > 180) {
bisector = (bisector + 180) % 360;
}
var beginangle = bisector - sectorAngle / 2;
if (beginangle < 0) {
beginangle += 360;
}
var endangle = (bisector + sectorAngle / 2) % 360;
var sectorOptions = jQuery.extend({}, drawOptions, {
startAngle: beginangle,
stopAngle: endangle
});
return L.circle(centrept, sectorRadius, sectorOptions);
}
function zapAirspace() {
if (mapLayers.airspace) {
map.removeLayer(mapLayers.airspace);
}
}
function showAirspace() {
var i;
var polyPoints;
var airStyle = {
"color": "black",
"weight": 1,
"opacity": 0.20,
"fillColor": "red",
"smoothFactor": 1
};
var suafeatures = [];
zapAirspace();
if ((airClip > 0) && (map.getZoom() > 6)) {
for (i = 0; i < airspace.polygons.length; i++) {
if (airspace.polygons[i].base < airClip) {
polyPoints = airspace.polygons[i].coords;
suafeatures.push(L.polygon(polyPoints, airStyle));
}
}
for (i = 0; i < airspace.circles.length; i++) {
if (airspace.circles[i].base < airClip) {
suafeatures.push(L.circle(airspace.circles[i].centre, 1000 * airspace.circles[i].radius, airStyle));
}
}
mapLayers.airspace = L.layerGroup(suafeatures).addTo(map);
}
}
// End of private methods
var map = L.map(elementName);
//Airspace clip altitude and initial bounds now a property of this object
var airClip = 0;
var pin;
var initBounds;
var airspace = {};
var mapQuestAttribution = ' | Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png">';
var mapLayers = {
openStreetMap: L.tileLayer('http://otile1.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.jpg', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>' +
mapQuestAttribution,
maxZoom: 18
}),
photo: L.tileLayer('http://otile1.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg', {
attribution: 'Portions Courtesy NASA/JPL-Caltech and U.S. Depart. of Agriculture, Farm Service Agency' +
mapQuestAttribution,
maxZoom: 11
})
};
var layersControl = L.control.layers({
'MapQuest OpenStreetMap': mapLayers.openStreetMap,
'MapQuest Open Aerial (Photo)': mapLayers.photo
});
mapLayers.openStreetMap.addTo(map);
layersControl.addTo(map);
var trackLatLong = [];
var timePositionMarker;
L.AwesomeMarkers.Icon.prototype.options.prefix = 'fa';
var planeIcon = L.AwesomeMarkers.icon({
icon: 'plane',
iconColor: 'white',
markerColor: 'red'
});
return {
reset: function() {
// Clear any existing track data so that a new file can be loaded.
if (mapLayers.track) {
map.removeLayer(mapLayers.track);
layersControl.removeLayer(mapLayers.track);
}
if (mapLayers.task) {
map.removeLayer(mapLayers.task);
layersControl.removeLayer(mapLayers.task);
}
if (pin) {
map.removeLayer(pin);
}
},
setAirspace: function(suadata) {
airspace = suadata;
showAirspace();
},
addTrack: function(latLong) {
trackLatLong = latLong;
var trackLine = L.polyline(latLong, {
color: 'blue',
weight: 4
});
timePositionMarker = L.marker(latLong[0], {
icon: planeIcon
});
mapLayers.track = L.layerGroup([
trackLine,
timePositionMarker
]).addTo(map);
layersControl.addOverlay(mapLayers.track, 'Flight path');
initBounds = (trackLine.getBounds());
map.fitBounds(initBounds);
},
zoomToTrack: function() {
map.fitBounds(initBounds);
},
zapTask: function() {
if (mapLayers.task) {
map.removeLayer(mapLayers.task);
layersControl.removeLayer(mapLayers.task);
}
if (pin) {
map.removeLayer(pin);
}
},
addTask: function(coordinates, names, sectordefs) {
var taskLayers = [L.polyline(coordinates, {
color: 'dimgray'
})];
var lineDrawOptions = {
fillColor: 'green',
color: 'black',
weight: 2,
opacity: 0.8
};
var sectorDrawOptions = {
fillColor: 'green',
fillOpacity: 0.1,
color: 'black',
weight: 1,
opacity: 0.8
};
var j;
for (j = 0; j < coordinates.length; j++) {
taskLayers.push(L.marker(coordinates[j]).bindPopup(names[j]));
switch (j) {
case 0:
var startline = getLine(coordinates[0], coordinates[1], sectordefs.startrad, lineDrawOptions);
taskLayers.push(startline);
break;
case (coordinates.length - 1):
if (sectordefs.finishtype === "line") {
var finishline = getLine(coordinates[j], coordinates[j - 1], sectordefs.finrad, lineDrawOptions);
taskLayers.push(finishline);
}
else {
taskLayers.push(L.circle(coordinates[j], sectordefs.finrad * 1000, sectorDrawOptions));
}
break;
default:
if (sectordefs.use_barrel) {
taskLayers.push(L.circle(coordinates[j], sectordefs.tprad * 1000, sectorDrawOptions));
}
if (sectordefs.use_sector) {
var tpsector = getTpSector(coordinates[j], coordinates[j - 1], coordinates[j + 1], sectordefs.sector_rad * 1000, sectordefs.sector_angle, sectorDrawOptions);
taskLayers.push(tpsector);
}
}
}
mapLayers.task = L.layerGroup(taskLayers).addTo(map);
layersControl.addOverlay(mapLayers.task, 'Task');
},
updateAirspace: function(clip) {
airClip = clip;
showAirspace();
},
setClipAlt: function(clip) {
airClip = clip;
},
showTP: function(tpoint) {
map.setView(tpoint, 13);
},
pushPin: function(coords) {
var pinIcon = L.icon({
iconUrl: 'pin.png',
iconSize: [33, 53], // size of the icon
iconAnchor: [0, 50], // point of the icon which will correspond to marker's location
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
});
if (pin) {
map.removeLayer(pin);
}
pin = L.marker(L.latLng(coords), {
icon: pinIcon
}).addTo(map);
},
setTimeMarker: function(timeIndex) {
var markerLatLng = trackLatLong[timeIndex];
if (markerLatLng) {
timePositionMarker.setLatLng(markerLatLng);
if (!map.getBounds().contains(markerLatLng)) {
map.panTo(markerLatLng);
}
}
}
};
}