簡體   English   中英

位置管理器無法獲取位置

[英]Location Manager not getting location

我正在嘗試將我的應用程序之一更新為IOS9和Xcode 7.2.1。 位置管理器在較早的版本中工作,但現在不起作用。 沒有錯誤或警告...什么都沒有。 看來我的代碼沒有到達“ didUpdateLocations”。 我的代碼如下;

- (void)setupViewController {

    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
    {
        [self.locationManager requestAlwaysAuthorization];
    }

    locationManager.desiredAccuracy = 100;
    //locationManager.requestLocation;
    [locationManager startUpdatingLocation];


    //return coordinate;

    [self performSelector:@selector(stopUpdatingLocation:) withObject:@"Timed Out" afterDelay:5];       

}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"Doesn't get here.");

    self.bestEffortAtLocation = newLocation;
    latitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
    longitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];

There is other code here that gets city, country, etc.

此時不知道該怎么辦。 我一直在尋找類似的問題,但沒有發現任何東西。

問題是這一行:

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

永遠不會調用該方法,因為它不對應任何委托方法 您需要實現此方法:

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations {

更新您的info.plist文件。

<key>NSLocationWhenInUseUsageDescription</key>
<string>This application requires location services to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>

像這樣嘗試。

- (void)setupViewController
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = 100;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

    [self.locationManager startUpdatingLocation];
    [self.locationManager requestWhenInUseAuthorization];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    self.bestEffortAtLocation = newLocation;
    latitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
    longitudeString = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];

    [self.locationManager stopUpdatingLocation];
}

setupViewController刪除performselector方法,然后必須使用所有place self 希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM