4
在我的應用程序中,我想做geofencing。我有位置列表,爲此我設置了地理限制區域。 所有地點都有半徑100米。以下是代碼設置區域:Geofencing無法正常工作
CLLocationCoordinate2D cord;
cord.latitude = [location.latitude doubleValue];
cord.longitude = [location.longtitude doubleValue];
CLLocationDistance regionRadius = location.radius;
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:cord radius:regionRadius identifier:[NSString stringWithFormat:@"%d",location.locId]];
[appDelegate.locationManager startMonitoringForRegion:region];
我的問題是,它不能正常工作。對於調試,在didEnterRegion委託增加距離計算之間的當前位置和地區的位置有時這個距離是1500米。
以下是計算當前的位置和區域位置之間的距離代碼:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
CLLocation *loc1 = locationManager.location;
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:region.center.latitude longitude:region.center.longitude];
double dist = [loc1 distanceFromLocation:loc2];
}
任何想法,爲什麼-locationManager:didEnterRegion:
被觸發這種錯誤的方式?
謝謝回答瑞奇。正如你建議檢查當前位置和地區,我做了距離計算。你有沒有找到任何理由這種錯誤的位置管理者的行爲? – OMK
沒問題。您可以使用** [區域containsCoordinate:theLocationCoordinate]; **在觸發事件之前雙擊確認它是否真的在區域內。我在各種網站上搜索過,我找不到任何東西。我認爲這可能是由於某個國家可能發生的大半徑(準確性)不好的位置。我無法證實這一點。 – Ricky