-
Notifications
You must be signed in to change notification settings - Fork 70
/
index.html
182 lines (165 loc) · 6.15 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>arc.js</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="./arc.js"></script>
<style>
body {
margin: 0px;
padding: 0px;
font:12px/15px 'Helvetica';
}
.box {
display: flex;
height: 100vh;
}
#map {
flex-grow: 6;
}
#json-dump {
height: 80vh;
}
#geojson {
flex-grow: 1;
display: flex;
flex-direction: column;
}
#text {
align-self: center;
}
</style>
</head>
<body>
<div class="box">
<div id="map"></div>
<div id="geojson">
<h3 id="text">
<a href="https://github.com/springmeyer/arc.js">Arc.js with Leaflet</a>: click to plot arcs
</h3>
<textarea id="json-dump"></textarea>
</div>
</div>
<script>
var map = L.map('map').setView(new L.LatLng(0, 0), 2)
var idx = 1;
var features = [];
var url = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var attribution = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
L.tileLayer(url, {
attribution,
maxZoom: 17,
noWrap:true
}).addTo(map);
// number of intermediate arc points
var npoints = 100;
var offset = 20;
var coords = [];
var points = [];
var snap_tolerance = 500000;
map.on('click', onMapClick);
var start;
var end;
function draw(coords) {
var len = coords.length;
if (len == 1) {
var hit = false;
start = coords[0];
for (var i=0;i<points.length;++i) {
var distance = points[i].distanceTo(start);
if (distance<snap_tolerance) {
//console.log('hit previous point, re-using location')
start = points[i];
hit = true;
} else {
//console.log(distance);
}
}
if (!hit) {
points.push(start);
}
var circleOptions = {color: '#f03', opacity: 0.5, clickable:false};
var circle = new L.CircleMarker(start, circleOptions);
map.addLayer(circle);
} else if (len == 2) {
var hit = false;
end = coords[1];
for (var i=0;i<points.length;++i) {
var distance = points[i].distanceTo(end);
if (distance<snap_tolerance && (points[i].lng !== start.lng)) {
//console.log('hit previous point, re-using location')
end = points[i];
hit = true;
} else {
//console.log(distance);
}
}
if (!hit) {
points.push(end);
}
var circleOptions = {color: '#f03', opacity: 0.5, clickable:false};
var circle = new L.CircleMarker(end, circleOptions);
map.addLayer(circle);
try {
var from = {x: start.lng, y: start.lat};
var to = {x: end.lng, y:end.lat};
var properties = {
arc:idx++,
start:''+from.x+','+from.y,
end:''+to.x+','+to.y
};
var greatCircle = new arc.GreatCircle(from,to,properties);
} catch (e) {
// catch possible antipodes error
alert(e.message);
coords.length = 0;
return;
}
var gc = greatCircle.Arc(npoints,{offset:offset});
var line = new L.geoJson().addTo(map);
var geojson_feature = gc.json();
features.push(geojson_feature)
// TODO - figure out how to disable interactivity on json lines
line.addData(geojson_feature);
line.bindPopup("great circle from " + coords[0] + " to " + coords[1]);
setTimeout(function() {
map.addLayer(line);
coords.length = 0;
document.getElementById("json-dump").value = JSON.stringify(
{ "type": "FeatureCollection",
"features": features
});
},0)
}
}
function onMapClick(e) {
var coord = new L.LatLng(e.latlng.lat, e.latlng.lng);
//console.log('clicked at: ' + e.latlng.lng + ' ' + e.latlng.lat)
coords.push(coord.wrap());
draw(coords);
}
function add(s,e) {
var start_ll = new L.LatLng(s[1],s[0]);
var end_ll = new L.LatLng(e[1],e[0]);
var start_coord = {x: start_ll.lng, y:start_ll.lat};
var end_coord = {x:end_ll.lng, y:end_ll.lat};
var description = ''+s[0]+','+s[1]+'=>'+e[0]+','+e[1]+'';
var gc0 = new arc.GreatCircle(start_coord,end_coord, {'name': 'line', 'color':'#ff7200','description':description});
var line0 = gc0.Arc(npoints,{offset:offset});
var geojson = line0.json();
console.log(JSON.stringify(geojson))
document.getElementById("json-dump").value = JSON.stringify(geojson);
L.geoJson(geojson, {
style: function (feature) {
return {color: feature.properties.color};
},
onEachFeature: function (feature, layer) {
layer.bindPopup(feature.properties.description);
}
}).addTo(map);
}
</script>
</body>
</html>