0

私が到達しようとしているのは、都市名で注釈を表示することです。

だから私はクラス 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];

}

今、私にはよくわからない2つの問題があります:

  1. reverseGeo を data member として配置するのは正しいですか、それともイニシャライザ内に割り当てて didFindPlacemark/didFailWithError デリゲート内で解放する方がよいでしょうか (そこで解放することさえ可能ですか) ?

  2. 注釈が表示されたときに、reverseGeo が回答 (目印またはエラー - それが何であれ) を返したことを確認するにはどうすればよいですか。たぶん、ネットワークの応答を待つのは間違っているので、そのままにしておく必要があります-ネットワークの応答がいつ/いつ到着するかがわからないため、それに応じてMapView内のannotationViewが更新されます。

できる限り詳しく教えてください。ありがとう

4

1 に答える 1

0
  1. データメンバとして保存しても問題ありません。

  2. ユーザーの現在地に注釈の痕跡を残しているように見えますか? 通常のユーザーの現在位置の注釈を、ユーザーがどこにいたかを示す「パンくずリスト」で補足している場合は、注釈が返されるまでポイントをマップに追加するのを待つ必要があります (それが動作である場合)。あなたがしたい)。マップを管理するクラスを 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.
于 2010-08-15T00:36:34.033 に答える