2011-12-23 34 views
0

我試圖將城市&狀態信息添加到MKAnnotation,但是在將數據添加到註釋之前數據正在清除。這裏是我當前的代碼(只顯示爲簡單起見關注的章節/部分):使用CLGeocoder向MKAnnotation添加城市和州信息

實施:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{  
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; 
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
    CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
    [self setCity:[placemark locality] andState:[placemark administrativeArea]]; 
    }]; 

    NSLog(@"Location 1: %@, %@", city, state); 

    MapPoint *mp = [[MapPoint alloc] initWithCoordinate:[newLocation coordinate] 
                title:[locationTitleField text] 
                date:[dateFormat stringFromDate:today]]; 

    [mapView addAnnotation:mp]; 
} 

- (void)setCity:(NSString *)c andState:(NSString *)s 
{ 
    [self setCity:c]; 
    [city retain]; 
    [self setState:s]; 
    [state retain]; 
    NSLog(@"Location 2: %@, %@", city, state); 
} 

接口:

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate, MKMapViewDelegate> 
{ 
    CLLocationManager *locationManager; 
    IBOutlet MKMapView *mapView; 
    NSString *city; 
    NSString *state; 
} 

@property (strong, nonatomic) IBOutlet UIWindow *window; 
@property (nonatomic, copy) NSString *city; 
@property (nonatomic, copy) NSString *state; 

- (void)setCity:(NSString *)c andState:(NSString *)s; 

@end 

位置1張打印(空)( null),而位置2打印加利福尼亞州舊金山。爲什麼數據在我可以在我的註釋中使用之前從這些屬性中刪除?

許多感謝...

+0

你在哪裏設置MKAnnotation? – 2011-12-23 02:19:49

+0

我添加了一些代碼來顯示我設置MKAnnotation的位置。類MapPoint符合MKAnnotation協議。不要擔心日期的事情。如果我在做出註釋之前能夠獲得城市和州,我會將其添加到副標題中。 – 2011-12-23 03:05:53

+0

這解釋了這個問題。請參閱我答案中的更新部分。 – 2011-12-23 03:15:52

回答

1

原因位置1是空的是,在運行時的反向地理編碼尚未完成。在地理編碼器完成後,您的completionHandler中的所有內容都將發生。您的位置1的日誌語句不是該completionHandler的一部分,並在調用反向地理編碼之後立即執行。最終的結果是日誌調用發生在之前,您請撥打setCity:andState:

更新

您需要設置註釋在completionHandler塊。代碼塊中出現的代碼的發生時間順序和時間要晚於您的其他消息實現。您可能希望read about blocks from Apple's documentation或搜索互聯網。

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{  
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; 
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
     CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
     [self setCity:[placemark locality] andState:[placemark administrativeArea]]; 
     MapPoint *mp = [[MapPoint alloc] initWithCoordinate:[newLocation coordinate] 
                 title:[locationTitleField text] 
                 date:[dateFormat stringFromDate:today]]; 
     [mapView addAnnotation:mp]; 
    }]; 
} 

末更新

此外,它看起來就好像你可能沒有使用性能,以及你能。由於您有citystate屬性,因此您不需要citystate實例變量。

嘗試改變它是這樣:

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate, MKMapViewDelegate> 
{ 
    CLLocationManager *locationManager; 
    IBOutlet MKMapView *mapView; 
} 

在實施:

@synthesize city, state 
... 
- (void)setCity:(NSString *)c andState:(NSString *)s 
{ 
    [self setCity:c]; 
    [self setState:s]; 
    NSLog(@"Location 2: %@, %@", city, state); 
} 

你不需要因爲屬性的副本額外保留報表的價值。您還必須確保撥打self.cityself.state以獲取值。

+0

這些額外的保留是希望保持數據不被清除。那麼,如何獲取城市和州信息以便在我的MKAnnotation中使用?好像completionHandler塊是代碼中運行的最後一件事,不管我如何構造它。 – 2011-12-23 03:00:47

+0

查看我更新的答案。 – 2011-12-23 03:16:40

+0

謝謝,大衛。我最終沒有打擾城市和州屬性。我只是通過以下方式將信息發送給MapPoint:[NSString stringWithFormat:@「%@,%@」,[placemark locality],[placemark administrativeArea]]; – 2011-12-23 17:05:22