【发布时间】:2014-12-26 13:35:54
【问题描述】:
我只是在玩 CLLocation 类。我成功完成了以下代码。当我按下按钮时,它会在 ALert msg 中显示位置。当我再次单击相同的按钮时,位置显示错误。而且我还注意到每次我必须去设置 - 隐私 - 位置服务以始终启用访问。当应用程序正确显示当前位置时,iPhone 设置中的位置访问会变为空白。我必须再次将其设置为 Always 才能运行该应用程序。我没有在 .plists 中添加任何内容。我只写了下面的代码。我的问题是将位置访问设置为始终后,它才第一次显示正确的位置
-(CLLocationCoordinate2D) getLocation{
CLLocationCoordinate2D coordinate;
if ([CLLocationManager locationServicesEnabled])
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
coordinate = [location coordinate];
}
return coordinate;
}
- (void)startTimer {
// if ([Run isEqualToString:@"can"]) {
i++;
NSLog(@"the i value %d", i);
Run =@"cant";
CLLocationCoordinate2D coordinate = [self getLocation];
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude]; //insert your coordinates
[ceo reverseGeocodeLocation:loc
completionHandler:^(NSArray placemarks, NSError error)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSDictionary *eventLocation = [NSDictionary dictionaryWithObjectsAndKeys:placemark.name,@"NAME",placemark.subLocality,@"SUBLOCALITY" ,placemark.postalCode,@"POSTALCODE" ,nil];
LocationDetails= [NSMutableDictionary dictionaryWithObjectsAndKeys:eventLocation,@"value", nil];
NSString *name=placemark.name;
NSString *subLocality=placemark.subLocality;
NSString *postalCode=placemark.postalCode;
NSMutableArray *listData;
if (!listData) listData = [[NSMutableArray alloc] init];
[listData addObject:LocationDetails];
NSLog(@"the Location details %@", listData);
NSString *location=[NSString stringWithFormat:@"You are now in %@ inside the sublocality of %@ and pin code is %@",name,subLocality,postalCode];
UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Location Update" message:location delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[Alert show];
}
];
// }
// contains the code you posted to start the timer
}
- (IBAction)getLocation:(id)sender {
[ self startTimer];
}
【问题讨论】:
标签: objective-c iphone ios8 cllocationmanager cllocation