-
Notifications
You must be signed in to change notification settings - Fork 5
/
vicinityMap.js
109 lines (83 loc) · 4.07 KB
/
vicinityMap.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
"use strict"
/* Copyright (c) 2014, Robert Buchholz <[email protected]>
The contents of this file are licensed under the GNU General Public License version 3
(see the LICENSE file in the project root for details)
*/
var VicinityMap = {
init: function(div, lat, lng)
{
if (!VicinityMap.map)
{
VicinityMap.map = L.map(div, {keyboard:false} );
VicinityMap.map.on("click", VicinityMap.onMapClick);
VicinityMap.map.on("touchstart", VicinityMap.onMapClick);
VicinityMap.map.on("zoomend", VicinityMap.renderFrustum);
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'OpenStreetMap',
maxZoom: 19, minZoom:0
}).addTo(VicinityMap.map);
L.control.scale({imperial:false, position:"topright"}).addTo(VicinityMap.map);
}
},
resetView: function( latlng)
{
VicinityMap.map.setView( {"lat": latlng.lat, "lng": latlng.lng}, 17);
},
updatePositionMarker: function(newPos)
{
if (!VicinityMap.map) //not yet initialized
return;
if (VicinityMap.positionMarker)
VicinityMap.map.removeLayer(VicinityMap.positionMarker);
VicinityMap.positionMarker = L.marker( {"lat": newPos.lat, "lng": newPos.lng} );
VicinityMap.positionMarker.addTo(VicinityMap.map);//.bindPopup("You are here");
},
renderFrustum: function()
{
if (!VicinityMap.map) //not yet initialized
return;
if (VicinityMap.frustum)
{
VicinityMap.map.removeLayer(VicinityMap.frustum);
VicinityMap.frustum = null;
}
var effectivePosition = Controller.getEffectivePosition();
/* One degree latitude on earth always corresponds to the same distance in meters ( 1/360th of the earth circumference).
* But the distance of one degree longitude changes depending of the current latitude.
* This aspect is the ration between the two distances. It is needed to correctly
* draw that viewing frustum, which needs to be specified in lat/lnt
*/
var localAspect = Math.cos( effectivePosition.lat / 180 * Math.PI);
var yawRad = Controller.viewAngleYaw / 180 * Math.PI;
/* compute only planar lookDir (ignoring pitch), as this is the relevant direction to render the frustum
on a 2D map
*/
var lookDir = [Math.sin( yawRad), Math.cos(yawRad)];
//console.log ("local aspect ratio at %s is %s", position.lat, localAspect );
//console.log( webGlCanvas.height, webGlCanvas.width, fieldOfView / webGlCanvas.height * webGlCanvas.width);
var phi = (0.5 * fieldOfView / webGlCanvas.height * webGlCanvas.width );
if (phi > 60) phi = 60; // wider FOV frustums look irritating
phi = phi / 180 * Math.PI;
var leftDir = [ Math.cos(phi) * lookDir[0] - Math.sin(phi) * lookDir[1],
Math.sin(phi) * lookDir[0] + Math.cos(phi) * lookDir[1] ];
var rightDir =[ Math.cos(-phi) * lookDir[0] - Math.sin(-phi) * lookDir[1],
Math.sin(-phi) * lookDir[0] + Math.cos(-phi) * lookDir[1] ];
var len = Math.pow(0.5, VicinityMap.map.getZoom())*2000;
//console.log(map.getZoom(), len);
var pA = { "lat": effectivePosition.lat + leftDir[1]*len*localAspect, "lng": effectivePosition.lng + leftDir[0]*len };
var pB = { "lat": effectivePosition.lat + rightDir[1]*len*localAspect, "lng": effectivePosition.lng + rightDir[0]*len};
//conversion for closure compiler
effectivePosition = {"lat": effectivePosition.lat, "lng": effectivePosition.lng};
var line = [effectivePosition, pA, pB, effectivePosition ]
VicinityMap.frustum = L.polygon(line, {"color": 'red', "noClip": 'true', "fillColor":"white", "fillOpacity":0.4}).addTo(VicinityMap.map);
},
onMapClick: function(e)
{
resetPosition(e["latlng"]);
},
onChangeSize: function()
{
if (VicinityMap.map)
VicinityMap.map.invalidateSize(false);
}
}