我试图通过使用核心位置框架来获取用户的位置。无论如何,它只是在连接互联网时提供位置和地址。我想通过GPS获取用户的位置。
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc]init];
geocoder = [[CLGeocoder alloc]init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
if([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
{
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError
*)error
{
NSLog(@"did fail with error %@",error);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation=newLocation;
NSLog(@"location : %@",currentLocation);
if(currentLocation !=nil)
{
self.latitudelabel.text = [NSString
stringWithFormat:@"%.8f",currentLocation.coordinate.latitude];
self.longitudelabel.text = [NSString
stringWithFormat:@"%.8f",currentLocation.coordinate.longitude];
}
CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];
NSLog(@"Calc.distance%f",distance);
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^
(NSArray *placemarks, NSError *error)
{
if(error == nil && [placemarks count]>0)
{
placemark = [placemarks lastObject];
NSLog(@"Country %@",placemark.country);
NSLog(@"Area %@",placemark.administrativeArea);
NSLog(@"City %@",placemark.locality);
NSLog(@"Code %@",placemark.postalCode);
NSLog(@"Road %@",placemark.thoroughfare);
NSLog(@"Number %@",placemark.subThoroughfare);
self.addresslabel.text = [NSString stringWithFormat:@"%@ %@
\n %@ %@ \n %@ \n %@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
[self.locationManager stopUpdatingLocation];
}
else
{
NSLog(@"tktk%@",error.debugDescription);
[self.locationManager stopUpdatingLocation];
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)btnPressed:(UIButton *)sender
{
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
if([[[UIDevice currentDevice]systemVersion]floatValue]>=8.0)
{
[self.locationManager requestAlwaysAuthorization];
}
[locationManager startUpdatingLocation];
}
@end
有什么建议吗?