wangrong
2021-11-24 130b72d6c5734f8acaac0214b6dfe5c8bfa319c3
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
export function isPointInCircle(point, circle = {}) {
  //检查类型是否正确
  if (!(point instanceof BMap.Point) || !(circle instanceof BMap.Circle)) {
    return false
  }
 
  //point与圆心距离小于圆形半径,则点在圆内,否则在圆外
  var c = circle.getCenter()
  var r = circle.getRadius()
 
  var dis = getDistance(point, c)
  if (dis <= r) {
    return true
  } else {
    return false
  }
}
 
function getDistance(point1, point2) {
  var EARTHRADIUS = 6370996.81
  //判断类型
  if (!(point1 instanceof BMap.Point) || !(point2 instanceof BMap.Point)) {
    return 0
  }
 
  point1.lng = _getLoop(point1.lng, -180, 180)
  point1.lat = _getRange(point1.lat, -74, 74)
  point2.lng = _getLoop(point2.lng, -180, 180)
  point2.lat = _getRange(point2.lat, -74, 74)
 
  var x1, x2, y1, y2
  x1 = degreeToRad(point1.lng)
  y1 = degreeToRad(point1.lat)
  x2 = degreeToRad(point2.lng)
  y2 = degreeToRad(point2.lat)
 
  return (
    EARTHRADIUS *
    Math.acos(
      Math.sin(y1) * Math.sin(y2) +
        Math.cos(y1) * Math.cos(y2) * Math.cos(x2 - x1)
    )
  )
}
function _getLoop(v, a, b) {
  while (v > b) {
    v -= b - a
  }
  while (v < a) {
    v += b - a
  }
  return v
}
function _getRange(v, a, b) {
  if (a != null) {
    v = Math.max(v, a)
  }
  if (b != null) {
    v = Math.min(v, b)
  }
  return v
}
 
function degreeToRad(degree) {
  return (Math.PI * degree) / 180
}