2017-01-31 25 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

相關問題