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
|
}
|