简体   繁体   English

IOS中的位置服务,提示

[英]Location Services in IOS, prompt

My application needs the location services to work, so therefore the user gets asked the question when the application is first run. 我的应用程序需要位置服务才能正常工作,因此首次运行该应用程序时会向用户询问问题。 If the user says 'OK' then it will run the whole application without it on, because it will continue doing its work. 如果用户说“ OK”,那么它将继续运行整个应用程序而不会继续运行,因为它将继续执行其工作。 So the first run it doesn't really bring anything on the view controller. 因此,第一次运行并不会真正给视图控制器带来任何好处。

When I press the go button in XCODE the second time, it works fine because the user has already authorised the use of location services to get lat/long. 当我第二次按XCODE中的“执行”按钮时,它可以正常工作,因为用户已经授权使用位置服务来获取经度/纬度。

Has anyone had this problem before ? 有人遇到过这个问题吗? Any advice ? 有什么建议吗?

In your viewDidLoad: 在您的viewDidLoad:

locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];

What you need to do, is update your view after the "didUpdateLocation" calls, and call "SetNeedsDisplay", so your view will draw itself to update. 您需要做的是在“ didUpdateLocation”调用之后更新视图,然后调用“ SetNeedsDisplay”,这样您的视图便会绘制自身以进行更新。 Like so: 像这样:

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{

    self.LabellLatitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.latitude];
    self.LabelLongitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.longitude];
    [self.view setNeedsDisplay];

}

Even better if you add a activity indicator intill the location updates. 如果您添加一个活动指示器直到位置更新,那就更好了。

Now, if your calling this from your "AppDelegate" class, you can use "NSNotificationCenter" to send message to your view, to update the labels, or view, or whatever you need, like so: 现在,如果您从“ AppDelegate”类中调用此方法,则可以使用“ NSNotificationCenter”将消息发送到视图,更新标签或视图,或任何您需要的对象,例如:

#pragma mark - locationManager:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdatedLocation" object:nil userInfo:dictionary];


} 

And on your UIVIewController ViewDidLoad, user this to register to the notificaitons: 然后在您的UIVIewController ViewDidLoad上,使用此用户注册通知:

 //Adding self to notification Center:
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"UpdatedLocation" object:nil];

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM