iOS:提醒用户特定地理位置区域(纬度,经度)
Posted
技术标签:
【中文标题】iOS:提醒用户特定地理位置区域(纬度,经度)【英文标题】:iOS : alert user for specific Geo location area (lat,long) 【发布时间】:2013-04-06 11:17:24 【问题描述】:我正在尝试为用户实现Geo location
的功能,我正在静态设置latitude
和longitude
信息,当应用程序启动时,如果用户在我出现的那个区域内一条消息“你已被联系到办公室”,否则“你要离开办公室”。我已经实现了下面的代码来实现这一点,我尝试通过步骤和车辆四处移动,但在这两种情况下它总是显示“你已经到达办公室”,但是我离那个位置 2 公里!我认为问题在于CLLocationManager
委托中的地理数据比较。
- (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...";
我正在努力完成以下事情!
-
如果用户输入了地理位置,他应该“警觉”。 --- 如何匹配位置?
如果在该地理位置内四处走动,则代码应监控此活动! --- 需要设置所需的精度属性吗?
我希望我的代码不断检查用户地理位置,我该怎么做? --- 需要调用
NSTimer
中的函数吗?
我在 SO 上发现了很多相同的问题,但没有人给出匹配的答案!有人请指导我是否朝着正确的方向前进,因为此代码未显示! :)
【问题讨论】:
【参考方案1】:听起来您应该改用区域监控,它会告诉您用户何时进入或退出圆形区域。使用startMonitoringForRegion:
设置它并实现CLLocationManagerDelegate
方法
– locationManager:didEnterRegion:
– locationManager:didExitRegion:
– locationManager:monitoringDidFailForRegion:withError:
– locationManager:didStartMonitoringForRegion:
如果您在输入错误的位置数据时遇到问题,请在locationManager:didUpdateLocations:
或locationManager:didUpdateToLocation:fromLocation:
中检查CLLocation
的年龄。如果超过 60 秒,请不要使用它。
【讨论】:
没问题。正确设置区域监控可能很棘手。查看 Xcode 中“Regions”的示例代码。 如果您查看文档查看器,您可以通过搜索找到示例代码。快速浏览一下,您所做的看起来不错,但radius:kCLDistanceFilterNone
应该使用实际的半径数字,例如 100(100 米)。以上是关于iOS:提醒用户特定地理位置区域(纬度,经度)的主要内容,如果未能解决你的问题,请参考以下文章