我試圖實現一個Geo location
爲用戶的功能,我靜態設置latitude
和longitude
信息時,如果用戶的應用程序啓動在那個區域內,我正在顯示一條消息,「你已經到達辦公室」,否則「你正在外出」。爲了達到這個目的,我已經實現了下面的代碼,我嘗試了通過步驟和車輛四處移動,但是在兩種情況下,它總是顯示「您已經到達辦公室」,但是我離該位置2公里!我認爲問題在於CLLocationManager
代表中地理數據的比較。iOS版:針對特定地理區域位置警告用戶(緯度,經度)
- (void) startUpdateUserLocation
{
if(!locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
// [locationManager startUpdatingLocation];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(latitude, longitude);
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:coord radius:kCLDistanceFilterNone identifier:@"identifier"];
[locationManager startMonitoringForRegion:region];
}
- (void)viewDidLoad
{
[super viewDidLoad];
latitude = 23.076289;
longitude = 72.508129;
}
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
MKCoordinateRegion region;
region.center = mapView.userLocation.coordinate;
region.span = MKCoordinateSpanMake(0.25, 0.25);
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
lblCurrentCoords.text = [NSString stringWithFormat:@"lat %f lon %f",mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude];
[self startUpdateUserLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
[listOfPoints addObject:manager.location];
[tablePoints reloadData];
/*
* locationManager:didEnterRegion:
*
* Discussion:
* Invoked when the user enters a monitored region. This callback will be invoked for every allocated
* CLLocationManager instance with a non-nil delegate that implements this method.
*/
lblLocationStatus.text = @"You're in office area!...";
}
- (void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
/*
* locationManager:didExitRegion:
*
* Discussion:
* Invoked when the user exits a monitored region. This callback will be invoked for every allocated
* CLLocationManager instance with a non-nil delegate that implements this method.
*/
lblLocationStatus.text = @"You're going out from office area!...";
}
- (void)locationManager:(CLLocationManager *)manager
didStartMonitoringForRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_TBD,__IPHONE_5_0)
{
/*
* locationManager:didStartMonitoringForRegion:
*
* Discussion:
* Invoked when a monitoring for a region started successfully.
*/
lblLocationStatus.text = @"Start monitoring...";
}
- (void)locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
withError:(NSError *)error __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0)
{
/*
* locationManager:monitoringDidFailForRegion:withError:
*
* Discussion:
* Invoked when a region monitoring error has occurred. Error types are defined in "CLError.h".
*/
lblLocationStatus.text = @"Stop monitoring...";
}
我正在努力完成以下工作!
- 如果用戶進入地理位置,他應該是「alert」。 ---如何匹配位置?
- 如果在該地理位置內移動,則代碼應該監視此活動! ---需要設置所需的精度屬性?
- 我希望我的代碼不斷地檢查用戶地理位置,我該怎麼做? ---需要撥打
NSTimer
的功能?
我發現很多關於這個問題都問過同樣的問題,但沒有人找到答案!有人請指導我是否朝着正確的方向前進,因爲這段代碼沒有顯示出來! :)
沒問題。正確設置區域監視可能非常棘手。例如,查看Xcode中「Regions」的示例代碼。 – 2013-04-06 11:30:22
如果您查看文檔查看器,可以通過搜索找到示例代碼。從一眼就可以看出,你所做的事情看起來很好,但是'radius:kCLDistanceFilterNone'應該使用半徑的實際數字,例如100(100米)。 – 2013-04-06 12:42:30