簡體   English   中英

iOS 8 Beta 4 CLLocationManager不起作用地標null

[英]iOS 8 Beta 4 CLLocationManager not working Placemark null

我的位置管理器中有一個Singleton類,並且反向地理編碼在Viewcontroller中以在應用程序中打印地址。

在新的iOS 8 Beta 4出現之前,一切工作正常,現在反向地理編碼不起作用-地標每次都返回null。

它可以在iOS 8模擬器上運行,但不能在安裝了Beta 4的iPhone 5S中運行。

我的Singleton .h

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@protocol LocationHandlerDelegate <NSObject>

@required

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

@end

@interface TSLocationHandler : NSObject <CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}
@property (nonatomic,strong) id<LocationHandlerDelegate> delegate;


+(id)getSharedInstance;
-(void)startUpdating;
-(void)stopUpdating;
-(void)requestWhenInUseAuthorization;

@end

我的Singleton .m

#import "TSLocationHandler.h"

static TSLocationHandler *DefaultManager;

@interface TSLocationHandler()

-(void)initiate;

@end


@implementation TSLocationHandler


+(id)getSharedInstance
{

    if (!DefaultManager) {

        DefaultManager=[[self allocWithZone:NULL]init];
        [DefaultManager initiate];  
    }
    return DefaultManager;
}
-(void)initiate{

    locationManager = [[CLLocationManager alloc]init];
    locationManager.delegate = self;
}

-(void)startUpdating{

    [locationManager startUpdatingLocation];
}

-(void) stopUpdating{

    [locationManager stopUpdatingLocation];
}

-(void)requestWhenInUseAuthorization
{

    float ver =[[[UIDevice currentDevice]systemVersion]floatValue];

    if (ver>=8.0) {
        [locationManager requestWhenInUseAuthorization];
    }else{
    }
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    if (!error==0) {
        UIAlertView *errorAlert =[[UIAlertView alloc]initWithTitle:@"Error" message:@"An error as ocurred during the process of retrieving your location. Please Make sure that you have internet and that you have granted the app with authorization to get your location." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [errorAlert show];
    }
} 
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    if  ([self.delegate respondsToSelector:@selector(locationManager:didUpdateLocations:)])
    {
        [self.delegate locationManager:manager didUpdateLocations:locations];
    }
}

@end

我的ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self hasInternet];

    //Core Location
    [[TSLocationHandler getSharedInstance]requestWhenInUseAuthorization];
    [[TSLocationHandler getSharedInstance]setDelegate:self];
    [[TSLocationHandler getSharedInstance]startUpdating];
}

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

    CLLocation *currentLocation = [locations lastObject];
    geoCoder=[[CLGeocoder alloc]init];
    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

        NSLog(@"PLACEMARK: %@",placeMark.thoroughfare);
        NSLog(@"ERROR %@", error);

        if (error == nil && [placemarks count] > 0) {
            placeMark = [placemarks lastObject];


            NSLog(@"PLACEMARK: %@", placeMark.thoroughfare);

            self.userLocation.text = [NSString stringWithFormat:@"%@, %@",placeMark.thoroughfare,placeMark.locality];

            [[TSLocationHandler getSharedInstance]stopUpdating];

            [self performSelector:@selector(espera) withObject:self afterDelay:4];
        } else {

            NSLog(@"ERROR %@", error);
            NSLog(@"PLACEMARK: %@", placeMark.thoroughfare);
            NSLog(@"GEOCODE: %@",geoCoder);
        }
   } ];
}

幾件事...

Beta 4中的iOS 8模擬器默認未為您的應用設置“允許位置訪問”,這對我來說似乎是個錯誤。 您必須轉到“設置”應用程序,依次轉到“隱私”>“位置”,打開“位置服務”,然后轉到“允許位置訪問”,選擇“始終”,然后選擇您的應用程序(就在“系統服務”上方),然后為允許位置訪問選擇“始終”。

其次,您應該仔細查看文檔,尤其是清單1.3 您正在立即調用地理位置服務,而不是等待“最近”更新。

完成所有這些操作后,地理定位就可以了。 必須使用“模擬器”菜單中的“調試/位置”功能來更改位置。 我通常設置一個自定義位置,然后選擇“城市自行車騎行”以強制執行didUpdateLocations。

另外,請注意,您不應將位置更改快於大約10秒鍾,並且期望地理位置響應,否則它將返回錯誤(例如URL無效),以防止服務器上的過度使用/ DoS。

對代碼進行如下所示的修改可能會更好:

CLLocation *currentLocation = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
    geoCoder = [[CLGeocoder alloc]init];
    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (placemarks != nil) {
            self.zip = [placemarks[0] postalCode];
            NSLog(@"Got a zip code: %@", self.zip);
        } else if(error.code == kCLErrorGeocodeFoundNoResult) {
            NSLog(@"%s, %@", __func__, @"No result yet.");
        } else {
            NSLog(@"%s, geocoding failed, erro: %@", __func__, error);
        }
    }];
}

請記住...如果您重置模擬器內容和設置或在模擬器中刪除您的應用,則需要再次更改設置。

暫無
暫無

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

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