gpt4 book ai didi

iOS 8 Beta 4 CLLocationManager 不工作 Placemark null

转载 作者:行者123 更新时间:2023-11-29 10:40:11 24 4
gpt4 key购买 nike

我的位置管理器有一个单例类,反向地理编码在我的 Viewcontroller 中,用于在应用程序中打印地址。

一切正常,直到新的 iOS 8 Beta 4 出现,现在反向地理编码不起作用——地标每次都返回 null。

它适用于 iOS 8 模拟器,但不适用于我安装了 beta 4 的 iPhone 5S。

我的单例.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

我的单例.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

我的 View Controller

- (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 模拟器不会为您的应用设置“允许位置访问”,这对我来说像是一个错误。您必须转到“设置”应用程序,转到“隐私”>“位置”,打开“位置服务”,然后转到“允许位置访问”,选择“始终”,然后选择您的应用程序(就在“系统服务”上方),然后选择“始终”以允许位置访问。

其次,你应该仔细看文档,特别是Listing 1.3 .您正在立即调用地理定位服务,而不是等待“最近”更新。

完成所有这些之后,地理定位就可以工作了。有必要使用 Simulator 菜单中的 Debug/Location 功能更改位置。我通常设置一个自定义位置,然后选择 City Bicycle Ride 以强制触发 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);
}
}];
}

请记住...如果您重置模拟器内容和设置或在模拟器中删除您的应用程序,您将需要再次更改设置。

关于iOS 8 Beta 4 CLLocationManager 不工作 Placemark null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24921660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com