【发布时间】:2013-03-15 06:30:45
【问题描述】:
我想根据经纬度找到用户的当前位置。以前我已经使用 MKReverseGeocoder 完成了它,但由于它在 ios5 中已被弃用,我使用 CLGeocoder。但无法获取当前位置。
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocation *currentLocation = newLocation;
locationLabelOne=newLocation.coordinate.latitude;
locationLabelTwo=newLocation.coordinate.longitude;
geocoder = [[CLGeocoder alloc] init];
[locationManager stopUpdatingLocation];
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
NSString *get = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
NSLog(@"%@",placemark);
}
当我 NSLog 地标返回 null。我哪里出错了?
【问题讨论】:
-
尝试在
CLLocation *currentLocation = newLocation;之后记录 currentLocation 以检查值是否已设置。您还可以调试每一行以查看值是否设置正确。 -
locationManager:didUpdateToLocation:fromLocation已弃用 iOS 6.0,您应该改用locationManager:didUpdateLocations:。 -
是的,当我记录当前位置时,我得到这个: +/- 5.00m(速度 -1.00 mps / course -1.00)@ 3/15/13,12:33: 00 PM 印度标准时间
-
@rohan-patel 你能告诉我现在如何在这个中传递纬度和经度吗:?
-
是的。我想弄清楚。但首先你能告诉我你是如何在 NSlog 中提高速度
(speed -1.00 mps / course -1.00)的吗?
标签: iphone ipad xcode4.5 cllocationmanager clgeocoder