我有一个视图控制器,它从应用程序委托中提取用户的纬度和经度坐标。这很好用,但我还需要用户的城市、州和时区。我知道我应该为此使用 CLGeocoder(请参阅最后一段代码),但不知道如何将它们组合在一起。我只需要城市、州和时区的 NSStrings。有人有任何指示或示例吗?谢谢!
在我的 App Delegate 中,我使用 CCLocationManager 来获取坐标,如下所示:
- (NSString *)getUserCoordinates
{
NSString *userCoordinates = [NSString stringWithFormat:@"latitude: %f longitude: %f",
locationManager.location.coordinate.latitude,
locationManager.location.coordinate.longitude];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
return userCoordinates;
}
- (NSString *)getUserLatitude
{
NSString *userLatitude = [NSString stringWithFormat:@"%f",
locationManager.location.coordinate.latitude];
return userLatitude;
}
- (NSString *)getUserLongitude
{
NSString *userLongitude = [NSString stringWithFormat:@"%f",
locationManager.location.coordinate.longitude];
return userLongitude;
}
在我的视图控制器中,我将用户的纬度和经度作为 NSString 获取:
NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLatitude];
NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLongitude];
我想获得城市、州和时区。我知道我需要 CLGeocoder,但不知道如何将它融合在一起:
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks,
NSError *error) {
for (CLPlacemark * placemark in placemarks) {
NSString *locality = [placemark locality];
}
}