iPhone每隔一段时间获取用户位置
我浏览了stackoverflow中的一些问题,但无法得到清晰的了解。我正在开发一个 iPhone 应用程序,该应用程序涉及通过 GPS 获取用户的位置并根据我获得的值进行处理。我只是想确定我应该如何实现这个?使用这种方法:
[locationManager startUpdatingLocation]
给我准确的位置,但它经常更新,这是我不想要的。我想定期获取用户的位置,即每 2 分钟获取一次。我该如何实施?
我想到的方法(10 秒间隔):
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
NSTimer *currentTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(theActionMethod) userInfo:nil repeats:YES];
}
以及计时器的操作方法:
- (void)theActionMethod {
[locationManager startUpdatingLocation];
}
和方法 didUpdatetoLocation:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.startingPoint = newLocation;
NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g\u00B0", newLocation.coordinate.latitude];
NSLog([@"Latitude : " stringByAppendingString:latitudeString]);
[latitudeString release];
[locationManager stopUpdatingLocation];
}
现在我得到一个始终保持不变的值。每 10 秒更新一次位置,但所有值保持不变(我尝试打印经度、水平精度、高度、垂直精度等)。所以请帮助我如何获取每 2 分钟的准确(至少有点准确,因为我知道 GPS 不准确)用户位置。
我还想知道如何实现监视区域和重要位置更改的事情。有人成功实施过吗?我做不到。我没有注意到任何变化(我在 didEnterRegion 中打印了一些内容,即使我来回进出该区域也从未打印过)。预先非常感谢。
I browsed through some of the questions in stackoverflow but I couldn't get a clear picture. I am working on an iPhone application that involves getting the user's location through GPS and processing based on the value that I got. I just wanted to make sure that how should I implement this? Using this method :
[locationManager startUpdatingLocation]
gives me the accurate location but it keeps on updating often that I do not want. I would like to get the user's location on a periodic basis.i.e., like every 2 minutes. How do I implement that?
The method I thought (10 second interval):
- (void)viewDidLoad {
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
NSTimer *currentTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(theActionMethod) userInfo:nil repeats:YES];
}
and in the action method for the timer :
- (void)theActionMethod {
[locationManager startUpdatingLocation];
}
and the method didUpdatetoLocation:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.startingPoint = newLocation;
NSString *latitudeString = [[NSString alloc] initWithFormat:@"%g\u00B0", newLocation.coordinate.latitude];
NSLog([@"Latitude : " stringByAppendingString:latitudeString]);
[latitudeString release];
[locationManager stopUpdatingLocation];
}
Now I am getting a value that remains the same throughout. Every 10s the location is updated but all values remain the same (I tried printing longitude, horizontal accuracy,altitude,vertical accuracy etc.). So please help me how to get the exact(atleast somewhat accurate because I know that GPS is not accurate)user's location for every 2 minutes.
And I would also like to know how to implement the monitoringRegions and significantlocationchange thing. Does anyone successfully implemented it? I couldn't do it. I did not notice any change (I printed something in didEnterRegion which was never printed even when I went back and forth in and out of the region ). Thanks a lot in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就做你已经在做的同样的事情。只需忽略委托方法并将
theActionMethod:
更改为您想要的locationManager.location
属性即可。Just do that same thing you're already doing. Just ignore the delegate method and change
theActionMethod:
to use thelocationManager.location
property however you want.