-
Notifications
You must be signed in to change notification settings - Fork 6
/
Circle.js
59 lines (48 loc) · 1.22 KB
/
Circle.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
define([
"dojo/_base/declare",
"./_base",
"./Placemark",
"./util/_base"
], function(declare, djeo, Placemark, u) {
var numPoints = 40;
function getCoords(center, radius) {
var c = center,
lon0 = Math.PI * c[0]/180,
lat0 = Math.PI * c[1]/180,
ratio = radius/u.earthRadius,
coords = []
;
// calculating circle points
for (var i=0; i<numPoints; i++) {
var angle = 2*Math.PI*i/numPoints,
// calculate circle points on the surface
lat = Math.asin( Math.sin(lat0)*Math.cos(ratio) + Math.cos(lat0)*Math.sin(ratio)*Math.cos(angle) );
lon = lon0 + Math.atan2(Math.sin(angle)*Math.sin(ratio)*Math.cos(lat0), Math.cos(ratio)-Math.sin(lat0)*Math.sin(lat))
;
coords.push([180*lon/Math.PI, 180*lat/Math.PI]);
}
coords.push([coords[0][0], coords[0][1]]);
return coords;
}
var C = declare([Placemark], {
getType: function() {
return "Circle";
},
getCoordsType: function() {
return "Polygon";
},
_getCoords: function() {
var coords = this.coords;
if (!coords) {
var coords = [ getCoords(this.center, this.radius) ];
this.coords = coords;
}
return coords;
}
});
// register the constructor
djeo.featureTypes.Circle = C;
// make getCoords available externally
C.getCoords = getCoords;
return C;
});