-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
144 lines (117 loc) · 3.42 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Leaflet Circle</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<style>
body {
padding: 0;
margin: 0;
}
html,
body,
#map {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="./GreatCircle/GreatCircleUtil.js"></script>
<script src="./GreatCircle/arc.js"></script>
<script>
var map = L.map('map', {
center: [31, 121],
zoom: 4
});
L.tileLayer('http://{s}.tiles.mapbox.com/v3/springmeyer.map-7jkxlsx1/{z}/{x}/{y}.png', {
attribution: ''
}).addTo(map);
//求两点之间大圆距离(也就是最短距离)
GreatCircleUtil.distance(30.7,122.72,28.49,127.64);
//求两点之间的方位
GreatCircleUtil.bearing(30.7,122.72,28.49,127.64);
// number of intermediate arc points
var idx = 1;
var features = [];
var npoints = 100;
var offset = 20;
var coords = [];
var points = [];
var snap_tolerance = 500000;
map.on('click',onMapClick);
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 draw(coors) {
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)
line.addData(geojson_feature);
}
}
</script>
</html>