2012-09-07 21 views
0
- (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); 
    }]; 
} 

我使用上面的代碼通過給出經度和緯度來獲取地址,但控件沒有輸入到geocoder方法中,它跳過了它。任何人都可以幫助我。CLGeocoder不能正常工作

在此先感謝。

回答

0

沒關係,控件沒有在方法中輸入,但它起作用,請注意它會起作用的輸出。

0

這是正常的,因爲reverseGeocodeLocation內部的代碼是在塊內執行的。 一個塊內的代碼的執行發生在另一個線程中,所以不會在主線程中執行,這就是爲什麼控件不會進入geocoder方法內部。