今天做项目有个需求是在地图上圈一个多边形区域,判断某个点是否在所圈多边形区域内。
js代码:
isContainsPoint: function (point) {
var count = this.featurePoints.length,
flag = false;
for (var i = 0, j = count - 1; i < count; j = i++) {
if (((point.lat >= this.featurePoints[i].lat && point.lat < this.featurePoints[j].lat) ||
(point.lat < this.featurePoints[i].lat && point.lat >= this.featurePoints[j].lat)) &&
(point.lng < ((this.featurePoints[j].lng - this.featurePoints[i].lng) * (point.lat - this.featurePoints[i].lat) /
(this.featurePoints[j].lat - this.featurePoints[i].lat) + this.featurePoints[i].lng))) {
flag = !flag;
}
}
return flag;
}
注意:point 是判断是否在多边形内的点
featurePoints是多边形的顶点坐标,不重复,有顺序(顺逆时针都可以)