2014-05-20 35 views
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:被觸發這種錯誤的方式?

回答

3

這是的iOS地區毛刺監測,我曾經面臨類似的問題與區域範圍設定,我已經找到一種方法來解決這個問題。

我已經在這裏張貼的問題,我已經回答了我自己,我有一個博客帖子,以及: -

  1. Region Monitoring Glitch on iOS 7 - Multiple Notifications at the same time
  2. iOS Region Monitoring and Location Manager
+1

謝謝回答瑞奇。正如你建議檢查當前位置和地區,我做了距離計算。你有沒有找到任何理由這種錯誤的位置管理者的行爲? – OMK

+1

沒問題。您可以使用** [區域containsCoordinate:theLocationCoordinate]; **在觸發事件之前雙擊確認它是否真的在區域內。我在各種網站上搜索過,我找不到任何東西。我認爲這可能是由於某個國家可能發生的大半徑(準確性)不好的位置。我無法證實這一點。 – Ricky