Как получить текущее название города пользователя?

Передайте их по каналу до sort и uniq. Это удаляет все дубликаты.

uniq -d дает только дубликаты, uniq -u дает только уникальные (дубликаты полос).

89
задан Bill the Lizard 21 May 2012 в 12:28
поделиться

3 ответа

Что вам нужно сделать, так это настроить CLLocationManager , который найдет ваши текущие координаты. С текущими координатами вам нужно использовать MKReverseGeoCoder , чтобы найти ваше местоположение.

- (void)viewDidLoad 
{  
    // this creates the CCLocationManager that will find your current location
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];
}

// this delegate is called when the app successfully finds your current location
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    // this creates a MKReverseGeocoder to find a placemark using the found coordinates
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    geoCoder.delegate = self;
    [geoCoder start];
}

// this delegate method is called if an error occurs in locating your current location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
 NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
}

// this delegate is called when the reverseGeocoder finds a placemark
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    MKPlacemark * myPlacemark = placemark;
    // with the placemark you can now retrieve the city name
    NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
}

// this delegate is called when the reversegeocoder fails to find a placemark
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
}
106
ответ дан 24 November 2019 в 07:10
поделиться

Вы должны получить текущее местоположение пользователя, а затем использовать MKReverseGeocoder для распознавания города.

Отличный пример можно найти в Руководстве по программированию приложений для iPhone , глава 8. Как только у вас есть местоположение, инициализируйте геокодер, установите делегата и прочтите страну с метки. Прочтите документацию для MKReverseGeocodeDelegate и создайте методы:

  • reverseGeocoder: didFindPlacemark:
  • reverseGeocoder: didFailWithError:

     MKReverseGeocoder * geocoder = [[MKReverseGeocoordination] initWith
    geocoder.delegate = self;
    [запуск геокодера];
    
2
ответ дан 24 November 2019 в 07:10
поделиться

Прочтите документацию для MKReverseGeocoder - документация, руководства и примеры приложений предоставлены Apple неспроста.

-10
ответ дан 24 November 2019 в 07:10
поделиться