我正在尝试从我在应用程序中获得的 Lat/Long 值反转地理编码位置,我想从这个坐标中找到城市名称、国家名称和 ISO。
我目前正在使用 CLLocationManager 通过以下代码获取实际位置信息:
//Auto geolocation and find city/country
locationManager.delegate=self;
//Get user location
[locationManager startUpdatingLocation];
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
//Get nearby address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
locatedAtcountry = placemark.country;
locatedAtcity = placemark.locality;
locatedAtisocountry = placemark.ISOcountryCode;
//Print the location to console
NSLog(@"Estas en %@",locatedAtcountry);
NSLog(@"Estas en %@",locatedAtcity);
NSLog(@"Estas en %@",locatedAtisocountry);
[cityLabel setText:[NSString stringWithFormat:@"%@,",locatedAtcity]];
[locationLabel setText:[NSString stringWithFormat:@"%@",locatedAtcountry]];
//Set the label text to current location
//[locationLabel setText:locatedAt];
}];
它工作得很好,但是,可以从我已经保存在设备中的 Long/Lat 值做同样的事情,而不是像实际代码中的当前位置?
更新和解决方案:
感谢 Mark的回答,我终于使用以下代码从保存的坐标中获取信息:
CLLocation *location = [[CLLocation alloc] initWithLatitude:37.78583400 longitude:-122.40641700];
[self.geoCoder reverseGeocodeLocation: location completionHandler:
^(NSArray *placemarks, NSError *error) {
//Get nearby address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
locatedAtcountry = placemark.country;
locatedAtcity = placemark.locality;
locatedAtisocountry = placemark.ISOcountryCode;
//Print the location to console
NSLog(@"Estas en %@",locatedAtcountry);
NSLog(@"Estas en %@",locatedAtcity);
NSLog(@"Estas en %@",locatedAtisocountry);
[cityLabel setText:[NSString stringWithFormat:@"%@",locatedAtcity]];
[locationLabel setText:[NSString stringWithFormat:@"%@",locatedAtcountry]];
//Set the label text to current location
//[locationLabel setText:locatedAt];
}];