2012-08-10 3 views
0

J'ai actuellement une application qui quand je place une annotation sur une carte, elle montre le titre et le sous-titre, avec le sous-titre montrant les informations lat/long/rayon.Géocodage inversé sous-titre annotation

-(NSString *)title{ 
    return @"Saved position"; 
} 

    - (NSString *)subtitle { 
     return [NSString stringWithFormat: @"Lat: %.4F, Lon: %.4F, Rad: %.1fm", 
     self.coordinate.latitude, self.coordinate.longitude, self.radius]; 
    } 

    -(id)initWithCoordinate:(CLLocationCoordinate2D) coor{ 
     self.coordinate=coor; 
     NSLog(@"%f,%f",coor.latitude,coor.longitude); 
     return self; 
    } 

Je voudrais changer la ligne de sous-titre pour qu'elle affiche une adresse. Je sais que c'est le géocodage inverse en utilisant CLGeocoder mais je ne suis pas sûr de savoir comment le faire correctement. J'ai regardé de nombreux exemples en ligne, et j'ai regardé le site Apple Dev, mais c'est un peu écrasante. Toute aide est vraiment appréciée par ce programmeur débutant.

Répondre

0

J'ai résoudre ce et je voulais partager la façon dont je l'ai fait:

RegionAnnotation.h

#import <MapKit/MapKit.h> 
#import <CoreFoundation/CoreFoundation.h> 

@interface RegionAnnotation : NSObject <MKAnnotation, CLLocationManagerDelegate> 

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

//Reverse Geocoding 
@property(nonatomic, readonly) CLPlacemark *administrativeArea;//The state 
@property(nonatomic, readonly) CLPlacemark *locality; //The city 
@property(nonatomic, readonly) CLPlacemark *subThoroughfare;//Street number 
@property(nonatomic, readonly) CLPlacemark *thoroughfare;//Street address 

@end 

RegionAnnotation.m

#import "RegionAnnotation.h" 
#import <AddressBookUI/AddressBookUI.h> 

@implementation RegionAnnotation 

@synthesize coordinate = _coordinate, subtitle = _subtitle, title = _title, administrativeArea = _administrativeArea, location = _location, locality = _locality, subThoroughfare = _subThoroughfare, thoroughfare = _thoroughfare; 

- (id)init 
{ 
    self = [super init]; 

    return self;  
} 

- (void) setCoordinate:(CLLocationCoordinate2D)newCoordinate 
{ 
    _coordinate = newCoordinate; 
} 

- (NSString *)title { 
    return @"Monitored Region"; 
} 

- (void) setSubtitle:(NSString *)newSubtitle 
{ 
    _subtitle = newSubtitle; 
} 

-(void)reverseGeocodeLocation:(CLPlacemark *)placemark 
{ 

    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

    CLLocationCoordinate2D coord = self.coordinate; 

    CLLocation *location = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude]; 

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { 
     CLPlacemark *placemark = [placemarks objectAtIndex:0]; 

     NSLog(@"%@ %@ %@ %@", 
       placemark.subThoroughfare, 
       placemark.thoroughfare, 
       placemark.locality, 
       placemark.administrativeArea); 
    }]; 
} 


- (NSString *)getAddress { 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
    CLLocation *location = [[CLLocation alloc] initWithLatitude:self.coordinate.latitude longitude:self.coordinate.longitude]; 
    NSString *geoAddress = [[NSString alloc] init]; 
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) { 
     NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!"); 
     if (error){ 
      NSLog(@"Geocode failed with error: %@", error); 
//   [self displayError:error]; 
      return; 
     } 
     NSLog(@"getAddress Received placemarks: %@", placemarks); 
     CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
     // use the AddressBook framework to create an address dictionary 
     NSString *addressString = ABCreateStringWithAddressDictionary(placemark.addressDictionary, NO);   
     _subtitle = addressString; 
    }]; 

    return geoAddress; 

} 


@end 

J'ai actuellement l'utilisateur faire une longue pression pour créer une annotation sur la carte. Maintenant, lorsque l'utilisateur sélectionne la broche en appuyant dessus, il montre l'adresse physique au lieu de la longitude et la latitude. J'espère que cela aidera tous ceux qui ont cherché un moyen de le faire.

Questions connexes