forked from springmeyer/arc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
156 lines (137 loc) · 5.56 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
<!DOCTYPE html>
<html>
<head>
<title>arc.js</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.css" />
<!--[if lte IE 8]>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.ie.css" />
<![endif]-->
<script src="http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.js"></script>
<script src="./arc.js"></script>
<style>
body {
margin:10px auto;
width:90%;
font:14px/20px 'Helvetica';
}
#map {
border: 1px solid;
width: 100%;
height: 420px;
margin: 10px auto;
}
#json-dump {
width: 100%;
height: 100px;
}
</style>
</head>
<body>
<div>
<h3>Arc.js with Leaflet</h3>
<p>Single clicks on the map will plot points. Once you have drawn two points a great circle arc (curved path) should automatically be drawn between the first point and the second. Nearby clicks will be snapped to existing start/end points. Click on the lines to get details. All drawn lines are collected as GeoJSON below the map.</p>
<p><a href="https://github.com/springmeyer/arc.js">source code on github</a></p>
</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-dn2wf053/{z}/{x}/{y}.png'
var osm = new L.TileLayer(url, {maxZoom: 17, attribution: attribution, noWrap:true});
map.setView(new L.LatLng(0, 0), 1).addLayer(osm);
// number of intermediate arc points
var npoints = 50;
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 = new arc.Coord(start.lng,start.lat);
var to = new arc.Coord(end.lng,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);
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);
}
</script>
</body>
</html>