0

私はこのコードを使用してシミュレーターを使用して場所を取得していますが、出力が得られません。また、誰かがこの解決策またはより良い代替解決策を提案してくれたら.\

 -(void)viewDidAppear:(BOOL)animated
 {
_locationManager.delegate=self;
 [_locationManager startUpdatingLocation];
[self.geoCoder reverseGeocodeLocation: _locationManager.location completionHandler:
 ^(NSArray *placemarks, NSError *error) {
      if (error) {
         return;
     }

     if (placemarks && placemarks.count > 0)
     {
         CLPlacemark *placemark = placemarks[0];

         NSDictionary *addressDictionary =
         placemark.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];

         NSString *Countrynsme = [addressDictionary
                                  objectForKey:(NSString *)kABPersonAddressCountryKey];

         _requestorAddressText.Text = address;
         _requestorCityText.text = city;
         _requestorPostalText.text = zip;
         _CountryrequestorText.text = Countrynsme;
         _requestorStateText.text = state;
         }

  }];

 [_locationManager stopUpdatingLocation];
}
4

1 に答える 1

3

CLLocationManager は非同期 API です。場所をジオコーディングする前に、CLLocationManager の結果を待つ必要があります。

CLLocationManagerDelegateを使用して、ロケーション マネージャーの更新のリッスンを開始します

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];
    if (interval < 0) {
        interval = -interval;
    }        
    // Reject stale location updates.
    if (interval < 30.0) {
        // Start geocoding
        [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
             // Use geocoded results
             ...
        }];
    }
    // If you're done with getting updates then do [manager stopUpdatingLocation]
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    // Handle error. Perhaps [manager stopUpdatingLocation]
}

次にviewDidAppear、場所の検索をブートストラップします。

- (void)viewDidAppear {
    // PS: You're missing a call to [super viewDidAppear]
    [super viewDidAppear];
    // Start lookup for location
    _locationManager.delegate=self;
    [_locationManager startUpdatingLocation];
}

PS: dealloc では、場所の更新を停止し、ジオコードをキャンセルして、locationManager のデリゲートを nil にすることを忘れないでください。

于 2013-06-04T02:07:54.633 に答える