xcode 使用 UIButton 的 CLLocationManager startUpdatingLocation
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6982284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CLLocationManager startUpdatingLocation using a UIButton
提问by Neelesh
This is the first time I am using CLLocationManager. I am not sure if i am doing the right thing. Correct me if i am wrong.
这是我第一次使用CLLocationManager. 我不确定我是否在做正确的事情。如果我错了,请纠正我。
I initialize the locationManager in my viewDidLoadMethod and tell the locationManager to startUpdatingLocation in the same method.
我在我的viewDidLoad方法中初始化 locationManager并告诉 locationManager 在相同的方法中 startUpdatingLocation 。
When the delegate receives
当委托人收到
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
[locationManager stopUpdatingLocation];
//do stuff with the coordinates
}
to avoid repeated calls to this delegate method.
以避免重复调用此委托方法。
I have a UIButtonwhere users can click to update the location
我有一个UIButton用户可以点击更新位置的地方
//called by user action
-(IBAction)updateLocation{
//start updating delegate
locationManager.delegate=self;
[locationManager startUpdatingLocation];
}
However when the location changes and when I click the UIbutton, the location coordinates donot change at all. :(
但是,当位置发生变化并且我单击 时UIbutton,位置坐标根本不会发生变化。:(
What am i doing wrong? Is this the right of doing or should i not stop the locationManager at all ?
我究竟做错了什么?这是正确的做法还是我根本不应该停止 locationManager?
Help would be appreciated.
帮助将不胜感激。
回答by GarlicFries
CLLocationManagercaches your last location and returns it as soon as you call -startUpdatingLocation. So, you are starting updates, receiving the old location, and then stopping updates.
CLLocationManager缓存您的最后一个位置并在您调用-startUpdatingLocation. 因此,您开始更新,接收旧位置,然后停止更新。
This isn't how -startUpdatingLocation/-stopUpdatingLocationare meant to be used. As I asked above, what's wrong with calling the delegate method multiple times? If you only want to get the location when the user taps a button, leave the CLLocationManagerupdating, and just check CLLocationManger's locationproperty when the user taps your button.
这不是应该如何-startUpdatingLocation/-stopUpdatingLocation使用。正如我上面所问的,多次调用委托方法有什么问题?如果您只想在用户点击按钮时获取位置,请保留CLLocationManager更新,并在用户点击您的按钮时检查CLLocationManger的location属性。
If the reason you're trying to avoid multiple calls to the delegate method is because you're worried about power consumption, etc., adjust the desiredAccuracyproperty of the CLLocationManagerwith something like: locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters.
如果你想避免多次调用委托方法的原因是因为你担心功耗等,调整desiredAccuracy的性质CLLocationManager的东西,如:locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters。
All told, it might look something like this...
总而言之,它可能看起来像这样......
.h file:
.h 文件:
@interface YourController : NSObject <CLLocationManagerDelegate>
@property (nonatomic, retain) CLLocationManager *locationMgr;
@property (nonatomic, retain) CLLocation *lastLocation;
- (IBAction)getNewLocation:(id)sender;
@end
.m file:
.m 文件:
@interface YourController
@synthesize locationMgr = _locationMgr;
@synthesize lastLocation = _lastLocation;
- (id)init
{
if (self = [super init]) {
self.locationMgr = [[CLLocationManager alloc] init];
self.locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
self.locationMgr.delegate = self;
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (!self.lastLocation) {
self.lastLocation = newLocation;
}
if (newLocation.coordinate.latitude != self.lastLocation.coordinate.latitude &&
newLocation.coordinate.longitude != self.lastLocation.coordinate.longitude) {
self.lastLocation = newLocation;
NSLog(@"New location: %f, %f",
self.lastLocation.coordinate.latitude,
self.lastLocation.coordinate.longitude);
[self.locationMgr stopUpdatingLocation];
}
}
- (IBAction)getNewLocation:(id)sender
{
[self.locationMgr startUpdatingLocation];
NSLog(@"Old location: %f, %f",
self.lastLocation.coordinate.latitude,
self.lastLocation.coordinate.longitude);
}
- (void)dealloc
{
[self.locationMgr release];
self.locationMgr = nil;
[self.lastLocation release];
self.lastLocation = nil;
[super dealloc];
}
@end
回答by Srikar Appalaraju
Am assuming you have included #import <CoreLocation/CoreLocation.h>framework to being with. This is the way you start getting location updates.
我假设你已经包含了#import <CoreLocation/CoreLocation.h>框架。这是您开始获取位置更新的方式。
CLLocationManager *locationMgr = [[CLLocationManager alloc] init];
locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
locationMgr.delegate = self;
[locationMgr startUpdatingLocation];
You are correct here. After this you start getting location updates, here in this delegate-
你在这里是正确的。在此之后,您开始获取位置更新,在此委托中 -
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// Handle location updates
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// Handle error
}

