我想检查是否有人进入分配的边界,然后我必须提醒用户,例如“您已输入”,以及用户何时离开“您已离开”。我正在使用.KML文件绘制边界,其中有多个纬度和经度。在这里,我附上截图。因此,我担心的是,我如何检测到任何人在此边界内进入并离开该边界。预先感谢 边界看起来像这样。红色线是边界。即使应用程序处于后台模式或终止模式,如何检查是否有人进入/离开/离开特定边界区域?
回答
使用地图反射。以下是使用地图当前可见矩形的示例。关于您的问题,您可以使用convertRegion:toRectToView:先将您的区域转换为MKMapRect。
MKMapPoint userPoint = MKMapPointForCoordinate(mapView.userLocation.location.coordinate);
MKMapRect mapRect = mapView.visibleMapRect; // find visible map rect
//MKMapRect mapRect = [self getMapRectUsingAnnotations:arrCordinate];//find custom map rect
BOOL inside = MKMapRectContainsPoint(mapRect, userPoint);
MKMapRect mapRect = mapView.visibleMapRect; 使用边界区域从多个纬度和经度
- (MKMapRect) getMapRectUsingAnnotations:(NSArray*)arrCordinate {
MKMapPoint points[[arrCordinate count]];
for (int i = 0; i < [arrCordinate count]; i++) {
points[i] = MKMapPointForCoordinate([arrCordinate[i] MKCoordinateValue]);
}
MKPolygon *poly = [MKPolygon polygonWithPoints:points count:[arrCordinate count]];
return [poly boundingMapRect];
}
谢谢你对我的问题的回复。在你的回答中,我认为我必须每次检查用户在maprect内部的位置。有没有其他的方式来检查,如果条件更多?例如,CLLocationManager拥有自己的方法didEnterRegion和didExitRegion。 – Birju
我没有任何自己的方法,但通过它可以使用didUpdateToLocation方法。 (CLLocation *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation; –
如果我必须每次检查didUpdateToLocation,那么它是否工作,如果我的应用程序处于kill模式? – Birju
试试这个代码创建自定义mapRect。这是基于绕组编号算法。这适用于复杂的形状,如红线。
typedef struct {
double lon;
double lat;
} LATLON;
// returns true if w/in region
bool chkInRegion(LATLON poi, int npoi, LATLON *latlon)
{
int wn = 0;
for (int i = 0 ; i < npoi-1 ; i++) {
if (latlon[i].lat <= poi.lat && latlon[i+1].lat > poi.lat) {
double vt = (poi.lat - latlon[i].lat)/(latlon[i+1].lat - latlon[i].lat);
if (poi.lon < (latlon[i].lon + (vt * (latlon[i+1].lon - latlon[i].lon)))) {
wn++;
}
} else if (latlon[i].lat > poi.lat && latlon[i+1].lat <= poi.lat) {
double vt = (poi.lat - latlon[i].lat)/(latlon[i+1].lat - latlon[i].lat);
if (poi.lon < (latlon[i].lon + (vt * (latlon[i+1].lon - latlon[i].lon)))) {
wn--;
}
}
}
return wn < 0;
}
// test data
LATLON llval[] = {
{100,100},
{200,500},
{600,500},
{700,100},
{400,300},
{100,100}
};
#define NLATLON (sizeof(llval)/sizeof(LATLON))
int main(int argc, char **argv) {
while (1) {
char buf[1024];
fprintf(stderr, "lon = ");
fgets(buf, sizeof(buf), stdin);
double lon = atof(buf);
fprintf(stderr, "lat = ");
fgets(buf, sizeof(buf), stdin);
double lat = atof(buf);
LATLON ltest;
ltest.lat = lat;
ltest.lon = lon;
if (chkInRegion(ltest, NLATLON, llval)) {
fprintf(stderr, "\n*** in region ***\n\n");
} else {
fprintf(stderr, "\n=== outside ===\n\n");
}
}
return 0;
}
区域范围设定将无法在复杂的多边形形状的区域工作。也许你可以用其他方法解决问题。例如,将区域划分为更小的CLCircularRegion,然后为必须显示所有那些locationManager:didEnterRegion:和locationManager:didExitRegion:回调的通知的情况开发集合逻辑。但请记住,每个应用只允许同时监控最多20个区域。
请参阅https://forums.developer.apple.com/thread/21323 phillippk1建议其他可能的方法。
戴奥斯,谢谢你的回答。我会检查并更新你的相同。 – Birju
- 1. 当进入虚拟现实模式时离开用户界面
- 2. 如何检查字符串是否处于特定模式?
- 3. 每次关闭或离开时终止应用程序会话
- 4. 离开进程组中终止函数
- 5. 离开临界区
- 6. Xbox不会离开开发模式
- 7. 如何检查,如果没有特殊人物离开
- 8. React原生socket.io在应用程序处于后台模式时断开连接
- 9. 停止后台处理(的AsyncTask),当用户离开应用程序
- 10. 如何检查IIS是否处于32位或64位模式
- 11. 检测应用程序是否在开发模式下运行
- 12. 如何检查应用程序是否在前台或后台
- 13. Chrome应用程序离线模式
- 14. 离子2 - 检测抖动设备时,应用程序在后台模式
- 15. 检测用户是否进入或离开地区 - 地理编码
- 16. 即使在应用程序终止后ios 8定期后台进程
- 17. 查看离开视图边界
- 18. 防止按键离开应用程序
- 19. 在离子中,应用程序如何检测通知区域的打开?
- 20. MPMoviePlayerController在全屏模式后以错误的方向离开应用程序
- 21. 如何从离子选项卡打开离子模式
- 22. 检查是否有人通过鼠标移动离开网站
- 23. 名称节点处于安全模式。不能离开
- 24. 离子应用程序开发是否有效?在Ionic Framework中是否有任何Android应用程序开发?
- 25. 检查鼠标是否离开窗口的特定部分
- 26. 从后台模式重新打开应用程序
- 27. 打开通知即使应用程序不活动或终止
- 28. Ruby RMagick格式转换总是离开边界
- 29. 检查特定进程是否处于活动状态并终止它
- 30. Vaadin Grid的进入/离开编辑模式的监听器?
已经有人问: http://stackoverflow.com/questions/35457480/determine-if-a-gps-location-is-within-a-gpx-track-segment –
可能的重复[确定是否GPS位置是在一个Gpx轨道段](http://stackoverflow.com/questions/35457480/determine-if-a-gps-location-is-within-a-gpx-track-segment) –
@AndreasOetjen谢谢你的评论。但似乎你提到的问题的答案已不复存在。我已经看到了这个问题,但似乎我使用.kml文件并使用Apple提供的kml解析器代码进行分析,而不使用.gpx文件。 – Birju