私が到達しようとしているのは、都市名で注釈を表示することです。
だから私はクラス 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つの問題があります:
reverseGeo を data member として配置するのは正しいですか、それともイニシャライザ内に割り当てて didFindPlacemark/didFailWithError デリゲート内で解放する方がよいでしょうか (そこで解放することさえ可能ですか) ?
注釈が表示されたときに、reverseGeo が回答 (目印またはエラー - それが何であれ) を返したことを確認するにはどうすればよいですか。たぶん、ネットワークの応答を待つのは間違っているので、そのままにしておく必要があります-ネットワークの応答がいつ/いつ到着するかがわからないため、それに応じてMapView内のannotationViewが更新されます。
できる限り詳しく教えてください。ありがとう