2010-08-14 21 views
0

我想要達到的是顯示與城市名稱的註釋。iPhone - MKReverseGeocoder - 如何確保我有結果返回

所以我有一個類的MapPoint:

@interface MapPoint : NSObject<MKAnnotation,MKReverseGeocoderDelegate> { 

    NSString* title; 
    NSString* cityName; 
    CLLocationCoordinate2D coordinate; 
    MKReverseGeocoder* reverseGeo; 
} 

@property (nonatomic,readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic,copy) NSString* title; 
@property (nonatomic,copy) NSString* cityName; 

-(id) initWithCoordinate:(CLLocationCoordinate2D)c tilte:(NSString*)t; 

@end 

我實現這樣的:

@implementation MapPoint 

@synthesize title,coordinate,cityName; 

-(id) initWithCoordinate:(CLLocationCoordinate2D)c tilte:(NSString*)t 
{ 
    [super init]; 
    coordinate = c; 
    reverseGeo = [[MKReverseGeocoder alloc] initWithCoordinate:c]; 
    reverseGeo.delegate = self; 
    [reverseGeo start]; 
    [self setTitle:t]; 
    return self; 

} 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 
{ 
    NSString* city = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressCityKey]; 
    NSString* newString = [NSString stringWithFormat:@"city-> %@",city]; 
    [self setTitle:[title stringByAppendingString:newString]]; 
} 

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{ 
    NSLog(@"error fetching the placemark"); 
} 

-(void)dealloc 
{ 
    [reverseGeo release]; 
    [cityName release]; 
    [title release]; 
    [super dealloc]; 
} 

@end 

然後,在我的CoreLocation委託我使用的MapPoint這樣的:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    MapPoint* mp = [[MapPoint alloc] initWithCoordinate:[newLocation coordinate] tilte:[locationTitleField text]]; 
    [mapView addAnnotation:mp]; 

    [mp release]; 

} 

現在,我有兩個問題我不確定:

  1. 它是正確的把reverseGeo作爲數據成員,或者一個更好的選擇是初始化裏面只是 頁頭,並釋放它的didFindPlacemark/didFailWithError代表內(它甚至有可能在那裏釋放它)?

  2. 我怎樣才能確保當我的註釋顯示我肯定知道reverseGeo帶回答(地標或錯誤 - 無論它是什麼)。 也許這只是錯誤的等待網絡響應,我應該離開它 - 我只是不確定何時/如果網絡響應將到達它將相應更新MapView中的annotationView。

請儘量詳細說明。 謝謝

回答

0
  1. 它可以作爲數據成員存儲它很好。

  2. 它看起來像你留下了用戶當前位置的註釋痕跡?如果您正在使用「bread crumb trail」來補充常規用戶的當前位置註釋,那麼您需要等待將該點添加到地圖中,直到獲得註釋爲止(如果這是行爲你要)。我要麼通過讓管理你的地圖的類成爲MKReverseGeocoder委託來實現這一點(並且讓它設置標題屬性,然後將註釋添加到地圖reverseGeocoder:didFindPlacemark中),或者添加一個地圖引用到你的MapPoint類並讓它自己添加到同一回調中的地圖。

順便說一句,對於MKReverseGeocoder文檔包括以下內容:

  • When you want to update the location automatically (such as when the user is moving), reissue the reverse-geocoding request only when the user's location has moved a significant distance and after a reasonable amount of time has passed. For example, in a typical situation, you should not send more than one reverse-geocode request per minute.
+0

這或多或少什麼,我想,但後來我開始思考我在寫我的帖子。 1.你看,獲取地標是一個網絡請求,我真的想等到我拿回來,而不是之前顯示註釋嗎?如果以後我會更新標題屬性它會反映在mapView? 2. 現在,我爲每個註解(MapPoint內部)分配一個新的MKReverseGeocoder,這是正確的方式嗎? 還是有辦法使用同一個,只是改變它的座標? – Idan 2010-08-15 05:45:41

+0

MKReverseGeocoder已在iOS5中棄用。如果您現在開始開發應用程序,則應該使用CLGeocoder,否則,您可能需要稍後再進行遷移。 – Axel 2012-05-28 10:03:21