CLGeocoder 不工作
Posted
技术标签:
【中文标题】CLGeocoder 不工作【英文标题】:CLGeocoder not working 【发布时间】:2012-09-07 06:20:29 【问题描述】:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[window addViewForTouchPriority:viewController.view];
if(self.locationManager==nil)
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.purpose = @"We will try to tell you where you are if you get lost";
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=500;
self.locationManager=locationManager;
geoCoder = [[CLGeocoder alloc] init];
if([CLLocationManager locationServicesEnabled])
[self.locationManager startUpdatingLocation];
return YES;
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
lati = [[NSString alloc] initWithFormat:@"%+.6f", newLocation.coordinate.latitude];
NSLog(@"address:%@",lati);
longi = [[NSString alloc] initWithFormat:@"%+.6f", newLocation.coordinate.longitude];
NSLog(@"address:%@",longi);
CLLocationCoordinate2D coord;
coord.latitude = [lati doubleValue];
coord.longitude = [longi doubleValue];
[geoCoder reverseGeocodeLocation: newLocation completionHandler: ^(NSArray *placemarks, NSError *error)
//Get nearby address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
//Print the location to console
NSLog(@"I am currently at %@",locatedAt);
address = [[NSString alloc]initWithString:locatedAt];
NSLog(@"address:%@",address);
];
我使用上面的代码通过给出纬度和经度来获取地址,但控制没有进入地理编码器方法,它跳过它。谁能帮帮我。
提前致谢。
【问题讨论】:
【参考方案1】:没关系,控件没有进入方法中,但是可以工作,注意输出它会工作。
【讨论】:
【参考方案2】:这很正常,因为 reverseGeocodeLocation 内的代码是在块内执行的。 块内代码的执行发生在另一个线程中,因此不在主线程中执行,这就是控件不进入geocoder方法内部的原因。
【讨论】:
以上是关于CLGeocoder 不工作的主要内容,如果未能解决你的问题,请参考以下文章