forked from zpxiaomi/arc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
180 lines (159 loc) · 6.22 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
<!DOCTYPE html>
<html>
<head>
<title>arc.js</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js?2"></script>
<script src="./arc.js"></script>
<style>
body {
margin: 0px;
padding: 0px;
font:12px/15px 'Helvetica';
}
#text {
padding-left: 40px;
display:inline-block;
}
#map {
float: left;
width: 70%;
height: 700px;
}
#json-dump {
float:right;
width: 28%;
margin: 0px;
padding: 0px;
height: 650px;
}
</style>
</head>
<body>
<div id="text">
<h3><a href="https://github.com/springmeyer/arc.js">Arc.js with Leaflet</a>: click to plot arcs</h3>
</div>
<div id="map"></div>
<div id="geojson">
<textarea id="json-dump"></textarea>
</div>
<script>
var map = new L.Map('map');
var idx = 1;
var features = [];
//var url = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var attribution = 'Map data © 2011 OpenStreetMap contributors';
var url = 'http://{s}.tiles.mapbox.com/v3/springmeyer.map-7jkxlsx1/{z}/{x}/{y}.png'
var osm = new L.TileLayer(url, {maxZoom: 17, attribution: attribution, noWrap:true});
map.setView(new L.LatLng(0, 0), 2).addLayer(osm);
// 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.lon+','+from.lat,
end:''+to.lon+','+to.lat
};
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>