2017-01-31 9 views
4

我想检查是否有人进入分配的边界,然后我必须提醒用户,例如“您已输入”,以及用户何时离开“您已离开”。我正在使用.KML文件绘制边界,其中有多个纬度和经度。在这里,我附上截图。因此,我担心的是,我如何检测到任何人在此边界内进入并离开该边界。预先感谢enter image description here 边界看起来像这样。红色线是边界。即使应用程序处于后台模式或终止模式,如何检查是否有人进入/离开/离开特定边界区域?

+0

已经有人问: http://stackoverflow.com/questions/35457480/determine-if-a-gps-location-is-within-a-gpx-track-segment –

+0

可能的重复[确定是否GPS位置是在一个Gpx轨道段](http://stackoverflow.com/questions/35457480/determine-if-a-gps-location-is-within-a-gpx-track-segment) –

+0

@AndreasOetjen谢谢你的评论。但似乎你提到的问题的答案已不复存在。我已经看到了这个问题,但似乎我使用.kml文件并使用Apple提供的kml解析器代码进行分析,而不使用.gpx文件。 – Birju

回答

0

使用地图反射。以下是使用地图当前可见矩形的示例。关于您的问题,您可以使用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]; 
} 
+0

谢谢你对我的问题的回复。在你的回答中,我认为我必须每次检查用户在maprect内部的位置。有没有其他的方式来检查,如果条件更多?例如,CLLocationManager拥有自己的方法didEnterRegion和didExitRegion。 – Birju

+0

我没有任何自己的方法,但通过它可以使用didUpdateToLocation方法。 (CLLocation *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation; –

+0

如果我必须每次检查didUpdateToLocation,那么它是否工作,如果我的应用程序处于kill模式? – Birju

0

试试这个代码创建自定义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; 
} 

enter image description here

0

区域范围设定将无法在复杂的多边形形状的区域工作。也许你可以用其他方法解决问题。例如,将区域划分为更小的CLCircularRegion,然后为必须显示所有那些locationManager:didEnterRegion:和locationManager:didExitRegion:回调的通知的情况开发集合逻辑。但请记住,每个应用只允许同时监控最多20个区域。

请参阅https://forums.developer.apple.com/thread/21323 phillippk1建议其他可能的方法。

+0

戴奥斯,谢谢你的回答。我会检查并更新你的相同。 – Birju

相关问题