“Name” of a CLPlacemark

烈酒焚心 提交于 2019-12-01 00:58:00
forgot

To answer your first question, use the areasOfInterest property of the CLPlacemark.

As for the second question, thats really up to you, and how you want to format the string. If you want to build it using strings from the addressDictionary property, then by all means do so. You can pick and choose the parts, and create a string all in the completion handler.

Also, you mentioned you want to create an annotation for displaying on a map. I personally subclass MKAnnotation, and use that to quickly create an annotation to display on a map.

MyAnnotation.h

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

@interface MyAnnotation : NSObject <MKAnnotation> {
    NSString *_name;
    NSString *_address;
    CLLocationCoordinate2D _coordinate;
}

@property (copy) NSString *name;
@property (copy) NSString *address;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;

@end

MyAnnotation.m

#import "MyAnnotation.h"

@implementation MyAnnotation
@synthesize name = _name;
@synthesize address = _address;
@synthesize coordinate = _coordinate;

- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate
{
    if ((self = [super init])) {
        _name = [name copy];
        _address = [address copy];
        _coordinate = coordinate;
    }
    return self;
}

- (NSString *)title
{
    return _name;
}

- (NSString *)subtitle
{
    return _address;
}

@end

Declare an NSMutableArray to hold all the annotations, and throw the initWithName:address:coordinate: in your completion handler, and you can quickly get an array of annotations you can add to your map.

NSString *address = @"1 stockton, san francisco, ca";    
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) 
{
   NSMutableArray *annotationArray = [NSMutableArray array];
   for (CLPlacemark *aPlacemark in placemarks)
   {
      NSString *nameString = // Get your name from an array created by aPlacemark.areasOfInterest;
      NSString *addressString = // Put your address string info you get from the placemark here.
      CLLocationCoordinate2D coordinate = aPlacemark.location.coordinate;
      MyAnnotation *annotation = [[MyAnnotation alloc] initWithName:nameString address:addressString coordinate:coordinate];
      [annotationArray addObject:annotation];
   }
   // Now save annotationArray if you want to use it later
   // Add it to your MapView with [myMapView addAnnotations:annotationArray];
}];

Hope that helps out!

You can use ABCreateStringWithAddressDictionary function from AddressBookUI framework to get the address string from CLPlacemark's "addressDictionary" property.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!