gpt4 book ai didi

ios - locationManager 没有更新 Location

转载 作者:行者123 更新时间:2023-11-28 20:18:31 24 4
gpt4 key购买 nike

我正在使用此代码获取位置,使用模拟器,但它没有给我任何输出。如果有人建议我解决这个问题或更好的替代解决方案。\

 -(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];
}

最佳答案

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 只是 Bootstrap 的位置查找:

- (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 的委托(delegate)。

关于ios - locationManager 没有更新 Location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16908705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com