我想檢查是否有人進入分配的邊界,然後我必須提醒用戶,例如「您已輸入」,以及用戶何時離開「您已離開」。我正在使用.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