"姓名"一个CLPlacemark [英] "Name" of a CLPlacemark

查看:143
本文介绍了"姓名"一个CLPlacemark的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解CLPlacemark以及何时/如何为添加到地图的引脚的标注创建信息。在我几年前在更多iOS 3开发中阅读之前,他们反向对地址进行地理编码并构建地址(街道,邮政编码,州等)。首先,我需要自己构建这个字符串吗?我试图找到如何获取某些已知事物的位置名称,例如在下面的代码中搜索苹果商店:

I'm trying to understand CLPlacemark and when/how to create information for a callout of a pin that is added to a map. Before what I read in More iOS 3 development a few years ago, they reverse geocoded an address and built the address (street, zip, state, etc). First, do I need to build this string myself? I was trying to find out how to get the name of a location for certain known things like searching for the apple store in the code below:

NSString *address = @"1 stockton, san francisco, ca";    
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {

    [placemarks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"obj description: %@", [obj description]);

        CLPlacemark *aPlacemark = (CLPlacemark *)obj;
        NSLog(@"%@", [aPlacemark.addressDictionary description]);
        NSLog(@"name: %@", ((CLPlacemark *)obj).name);
    }];
];

当我打印出描述时,我看到控制台说:

When I print out the description, I see that the Console says:

Apple Store, San Francisco, 1 Stockton St, San Francisco, CA  94108-5805, United States @ <+37.78584545,-122.40651752> +/- 100.00m, region (identifier <+37.78584545,-122.40652161> radius 18.96) <+37.78584545,-122.40652161> radius 18.96m

它在哪里获得Apple Store,旧金山的名字?我以为它将是CLPlacemark.name属性,但它是null。因此,在尝试弄清楚如何创建name属性时,我发现:

Where does it get the Apple Store, San Francisco, name? I thought it would be the CLPlacemark.name property, but that is null. So in trying to figure out how the name property is created, I found:

        NSLog(@"%@", [aPlacemark.addressDictionary description]);

我得到输出:

City = "San Francisco";
Country = "United States";
CountryCode = US;
FormattedAddressLines =     (
    "Apple Store, San Francisco",
    "1 Stockton St",
    "San Francisco, CA  94108-5805",
    "United States"
);
PostCodeExtension = 5805;
State = California;
Street = "1 Stockton St";
SubAdministrativeArea = "San Francisco";
SubLocality = "Union Square";
SubThoroughfare = 1;
Thoroughfare = "Stockton St";
ZIP = 94108;

从中可以看出,在addressDictionary的FormattedAddressLines键中,标题就在那里同样。

From this, all I can see is that in the FormattedAddressLines key of the addressDictionary, the title is there as well.

所以我想我的两个问题是:

So I guess my 2 questions are:

1)如何获取位置的名称有一个(即Apple Store)?

1) How do I get the name of a location if there is one (i.e. Apple Store)?

2)我是否需要再建立我的字符串,因为地址字典似乎已经为我做了这个?

2) Do I need to build my string anymore since it seems like the address dictionary does that for me already?

谢谢!

推荐答案

要回答您的第一个问题,请使用 areasOfInterest CLPlacemark的属性。

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

关于第二个问题,这真的取决于你,以及你想如何格式化串。如果要使用 addressDictionary 属性中的字符串构建它,那么一定要这样做。您可以选择和选择部件,并在完成处理程序中创建一个字符串。

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.

此外,您提到要创建用于在地图上显示的注释。我个人将MKAnnotation子类化,并使用它快速创建一个注释以显示在地图上。

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

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

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

声明一个NSMutableArray来保存所有注释,并在完成处理程序中抛出 initWithName:address:coordinate:,您可以快速获得可添加到地图中的注释数组。

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];
}];

希望有所帮助!

这篇关于&QUOT;姓名&QUOT;一个CLPlacemark的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆