我试图实现一个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