Ответ 1
Я бы начал с класса CLReverseGeocoder
.
fooobar.com/questions/65879/... получает текущий город и, вероятно, может быть адаптирован для вашего использования.
Я видел из другого вопроса здесь: Определить страну пользователя iPhone, чтобы можно было получить текущую страну, в которой находится пользователь iPhone.
И это довольно удобно для многих применений. Тем не менее, было бы возможно пойти еще глубже и сделать вывод из iOS (если у него есть информация), в котором находится штат или город, в котором находится пользователь?
Я полагаю, что обратные услуги геокодирования станут следующим шагом, если все будет невозможно. Существуют ли даже такие вещи, как реверсивная служба геокодирования, которую вы можете нанять для своего приложения?
Я бы начал с класса CLReverseGeocoder
.
fooobar.com/questions/65879/... получает текущий город и, вероятно, может быть адаптирован для вашего использования.
MKReverseGeocoder
устарел в iOS 5, теперь он CLGeocoder
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self.locationManager stopUpdatingLocation];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
.... = [placemark locality];
}
}];
}
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:21.1700
longitude:72.8300];
[geocoder reverseGeocodeLocation:newLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"Geocode failed with error: %@", error);
return;
}
if (placemarks && placemarks.count > 0)
{
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary =
placemark.addressDictionary;
NSLog(@"%@ ", addressDictionary);
NSString *address = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *zip = [addressDictionary
objectForKey:(NSString *)kABPersonAddressZIPKey];
NSLog(@"%@ %@ %@ %@", address,city, state, zip);
}
}];
Результат
{
City = Surat;
Country = India;
CountryCode = IN;
FormattedAddressLines = (
Surat,
Gujarat,
India
);
Name = Surat;
State = Gujarat;
}
2012-12-20 21:33:26.284 CurAddress[4110:11603] (null) Surat Gujarat (null)
Следующие коды могут быть легко доступны для полной информации.
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if(placemarks.count){
placeNameLabel.text = [placemarks[0] name];
streetNumberLabel.text = [placemarks[0] subThoroughfare];
streetLabel.text = [placemarks[0] thoroughfare];
neighborhoodLabel.text = [placemarks[0] subLocality];
cityLabel.text = [placemarks[0] locality];
countyLabel.text = [placemarks[0] subAdministrativeArea];
stateLabel.text = [placemarks[0] administrativeArea]; //or province
zipCodeLabel.text = [placemarks[0] postalCode];
countryLabel.text = [placemarks[0] country];
countryCodeLabel.text = [placemarks[0] ISOcountryCode];
inlandWaterLabel.text = [placemarks[0] inlandWater];
oceanLabel.text = [placemarks[0] ocean];
areasOfInterestLabel.text = [placemarks[0] areasOfInterest[0]];
}
}];