map组件 怎样判断用户位置是否在多边形范围内,用来判断签到逻辑
1. 绘制打卡范围
var points = new Array(
this.bd2gcjString(116.1820943298403, 39.750488978269416),
this.bd2gcjString(116.19165426654206, 39.750488978269416),
this.bd2gcjString(116.19165426654206, 39.74736124994377),
this.bd2gcjString(116.1820943298403, 39.74736124994377)
);
var polygonObj = new plus.maps.Polygon(points);
polygonObj.setStrokeColor( #0199FE );
polygonObj.setFillColor( #66faff );
polygonObj.setFillOpacity(0.5);
polygonObj.setLineWidth(3);
mapContent.$getAppMap().addOverlay(polygonObj);
2. 获取个人位置后,判断范围
mapContent.$getAppMap().getUserLocation(function(state, point) {
if (0 == state) {
that.latitude = point.latitude;
that.longitude = point.longitude;
console.log( 个人位置: + JSON.stringify(point));
var markObj = new plus.maps.Marker(point);
markObj.setIcon( static/ic_baidu_local.png );
markObj.setLabel( 当前所在位置 );
var bubble = new plus.maps.Bubble( 当前所在位置 );
markObj.setBubble(bubble);
mapContent.$getAppMap().addOverlay(markObj);
//判断是否在打卡范围内
if (that.IsPtInPoly(point, polygonObj)) {
that.locText = 在范围内 ;
} else {
that.locText = 在范围外 ;
}
}
});
3. 方法
IsPtInPoly(o, l) {
var t = l.getPath();
var h = t.length;
var n = true;
var j = 0;
var g = 2e-10;
var s, q;
var e = o;
s = t[0];
for (var f = 1; f <= h; ++f) {
if (e.equals(s)) {
return n;
}
q = t[f % h];
if (e.latitude < Math.min(s.latitude, q.latitude) || e.latitude > Math.max(s.latitude, q.latitude)) {
s = q;
continue;
}
if (e.latitude > Math.min(s.latitude, q.latitude) && e.latitude < Math.max(s.latitude, q.latitude)) {
if (e.longitude <= Math.max(s.longitude, q.longitude)) {
if (s.latitude == q.latitude && e.longitude >= Math.min(s.longitude, q.longitude)) {
return n;
}
if (s.longitude == q.longitude) {
if (s.longitude == e.longitude) {
return n;
} else {
++j;
}
} else {
var r = ((e.latitude - s.latitude) * (q.longitude - s.longitude)) / (q.latitude - s.latitude) + s.longitude;
if (Math.abs(e.longitude - r) < g) {
return n;
}
if (e.longitude < r) {
++j;
}
}
}
} else {
if (e.latitude == q.latitude && e.longitude <= q.longitude) {
var m = t[(f + 1) % h];
if (e.latitude >= Math.min(s.latitude, m.latitude) && e.latitude <= Math.max(s.latitude, m.latitude)) {
++j;
} else {
j += 2;
}
}
}
s = q;
}
if (j % 2 == 0) {
return false;
} else {
return true;
}
}
bd2gcjString(latitude, longitude) {
var x_pi = (3.14159265358979324 * 3000.0) / 180.0;
var x = latitude - 0.0065,
y = longitude - 0.006;
var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
return new plus.maps.Point(z * Math.cos(theta), z * Math.sin(theta));
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。如内容涉嫌侵权,请在本页底部进入<联系我们>进行举报投诉!
THE END














暂无评论内容