CLGeocoder无法正常工作

时间:2012-09-07 06:20:30

标签: iphone cllocationmanager clgeocoder

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

我使用上面的代码通过给出纬度和经度来获取地址,但是控制没有进入地理编码方法,它会跳过它。任何人都可以帮助我。

提前致谢。

2 个答案:

答案 0 :(得分:0)

没关系,控件没有输入方法,但它有效,请注意输出它会起作用。

答案 1 :(得分:0)

这是正常的,因为reverseGeocodeLocation中的代码是在块内执行的。 块内部代码的执行发生在另一个线程中,因此不在主线程中执行,这就是控件不进入地理编码器方法的原因。