- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
问题
我有一组地标信息(国家、城市等)和一对纬度/经度。我想用它来创建一个 MKPlacemark 对象。
讨论
看来这个类只能由
创建- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary
谁的文档状态状态
You can create placemark objects manually for entities for which you already have address information, such as contacts in the Address Book. Creating a placemark object explicitly avoids the need to query the reverse geocoder object for the same information.
完美!我已经进行了反向地理编码并希望避免这样的查询。我可以向字典中添加什么?
For a list of strings that you can use for the keys of this dictionary, see the “Address Property” constants in ABPerson Reference. All of the keys in should be at the top level of the dictionary.
显示相关键
const ABPropertyID kABPersonAddressProperty;
const CFStringRef kABPersonAddressStreetKey;
const CFStringRef kABPersonAddressCityKey;
const CFStringRef kABPersonAddressStateKey;
const CFStringRef kABPersonAddressZIPKey;
const CFStringRef kABPersonAddressCountryKey;
const CFStringRef kABPersonAddressCountryCodeKey;
这与 MKPlacemark 的基本特征相去甚远:
Accessing the Location Data
Accessing the Placemark Attributes
Accessing Geographic Information
Accessing Landmark Information
幸运的是,MKPlacemark 父类(super class)的实际头文件说明了地址字典的一些内容:
// address dictionary properties
@property (nonatomic, readonly) NSString *name; // eg. Apple Inc.
@property (nonatomic, readonly) NSString *thoroughfare; // street address, eg. 1 Infinite Loop
@property (nonatomic, readonly) NSString *subThoroughfare; // eg. 1
@property (nonatomic, readonly) NSString *locality; // city, eg. Cupertino
@property (nonatomic, readonly) NSString *subLocality; // neighborhood, common name, eg. Mission District
@property (nonatomic, readonly) NSString *administrativeArea; // state, eg. CA
@property (nonatomic, readonly) NSString *subAdministrativeArea; // county, eg. Santa Clara
@property (nonatomic, readonly) NSString *postalCode; // zip code, eg. 95014
@property (nonatomic, readonly) NSString *ISOcountryCode; // eg. US
@property (nonatomic, readonly) NSString *country; // eg. United States
@property (nonatomic, readonly) NSString *inlandWater; // eg. Lake Tahoe
@property (nonatomic, readonly) NSString *ocean; // eg. Pacific Ocean
@property (nonatomic, readonly) NSArray *areasOfInterest; // eg. Golden Gate Park
所以,我创建了一个字典,然后像这样传递它:
return [[[MKPlacemark alloc] initWithCoordinate:aLocation.coordinate addressDictionary:addressDictionary] autorelease];
不幸的是,毕竟,反省表明信息没有坚持:
NSLog(@"placemark %@ from %@", placemark, addressDictionary);
NSLog(@"has %@", placemark.thoroughfare);
打印
2012-01-31 20:14:22.545 [15450:1403] placemark <+___,-___> +/- 0.00m from {
administrativeArea = __;
postalCode = _____;
subAdministrativeArea = ___;
subThoroughfare = __;
thoroughfare = "_____";
}
2012-01-31 20:14:22.545[15450:1403] has (null)
所以,我快结束了。有没有人知道如何创建自己的 MKPlacemark?谢谢。
最佳答案
你可以继承MKPlacemark
:
在MyPlacemark.h
@interface MyPlacemark : MKPlacemark
extern NSString * const kCustomPlacemarkAddressThoroughfareKey;
extern NSString * const kCustomPlacemarkAddressSubThoroughfareKey;
extern NSString * const kCustomPlacemarkAddressLocalityKey;
extern NSString * const kCustomPlacemarkAddressSubLocalityKey;
extern NSString * const kCustomPlacemarkAddressAdministrativeAreaKey;
extern NSString * const kCustomPlacemarkAddressSubAdministrativeAreaKey;
extern NSString * const kCustomPlacemarkAddressPostalCodeKey;
extern NSString * const kCustomPlacemarkAddressCountryKey;
extern NSString * const kCustomPlacemarkAddressCountryCodeKey;
@end
在 MyPlacemark.m 中:
#import "MyPlacemark.h"
@implementation MyPlacemark
NSString * const kCustomPlacemarkAddressThoroughfareKey = @"thoroughfare";
NSString * const kCustomPlacemarkAddressSubThoroughfareKey = @"subThoroughfare";
NSString * const kCustomPlacemarkAddressLocalityKey = @"locality";
NSString * const kCustomPlacemarkAddressSubLocalityKey = @"subLocality";
NSString * const kCustomPlacemarkAddressAdministrativeAreaKey = @"administrativeArea";
NSString * const kCustomPlacemarkAddressSubAdministrativeAreaKey = @"subAdministrativeArea";
NSString * const kCustomPlacemarkAddressPostalCodeKey = @"postalCode";
NSString * const kCustomPlacemarkAddressCountryKey = @"country";
NSString * const kCustomPlacemarkAddressCountryCodeKey = @"countryCode";
- (NSString *)thoroughfare
{
return [self.addressDictionary objectForKey:kCustomPlacemarkAddressThoroughfareKey];
}
- (NSString *)subThoroughfare
{
return [self.addressDictionary objectForKey:kCustomPlacemarkAddressSubThoroughfareKey];
}
- (NSString *)locality
{
return [self.addressDictionary objectForKey:kCustomPlacemarkAddressLocalityKey];
}
- (NSString *)subLocality
{
return [self.addressDictionary objectForKey:kCustomPlacemarkAddressSubLocalityKey];
}
- (NSString *)administrativeArea
{
return [self.addressDictionary objectForKey:kCustomPlacemarkAddressAdministrativeAreaKey];
}
- (NSString *)subAdministrativeArea
{
return [self.addressDictionary objectForKey:kCustomPlacemarkAddressSubAdministrativeAreaKey];
}
- (NSString *)postalCode
{
return [self.addressDictionary objectForKey:kCustomPlacemarkAddressPostalCodeKey];
}
- (NSString *)country
{
return [self.addressDictionary objectForKey:kCustomPlacemarkAddressCountryKey];
}
- (NSString *)countryCode
{
return [self.addressDictionary objectForKey:kCustomPlacemarkAddressCountryCodeKey];
}
@end
它看起来很丑,但这是迄今为止我发现的唯一可行的方法。
关于ios - 以编程方式/手动创建 MKPlacemark/CLPlacemark,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9089985/
我有一个@State 属性,它使用我创建的结构(地标)进行了初始化,该结构接受了 MKPlacemark。 @State private var selectedLandmark: Landmark
我想知道如何获取用户当前的地址名称。我已经找到了它的坐标,但我需要找到用户当前所在城市的名称。使用 Google API 更好还是我应该使用 MKPlacemark 来查找它?我看到 MKRevers
我在设置地标的 title 和 subtitle 时遇到问题。 CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geoco
我正在尝试快速编写一个创建 MKMapItem 的函数,但我收到一个字符串错误。这是代码: func mapItem() -> MKMapItem { let addressDictionar
我的 map View 工作正常,但放置在 map 上的图钉标题为美国。如何更改此标题? MKCoordinateRegion thisRegion = {{0.0,0.0}, {0.0,0.
我正在研究一个使用 MKReverseGeocoder 将纬度、经度转换为地址的示例。但是当我收到数据并尝试获取结果时,我收到了如下字符串: NSString *street = [geocoder.
我可以通过将此代码应用于 MKReversegeocoder 来获取街道和城市 NSString* city = [placemark.addressDictionary valueForKey:@"
我使用了 LocationManager,你可以在这个 gitHub 上找到它 https://github.com/varshylmobile/LocationManager/blob/master
我想用 Apple 的 map 显示两个地方的路线。 对于这两个地方,我有名称和坐标。 MKMapItem *currentLocation = [[MKMapItem alloc] initWi
找不到类型 MKPlacemark 的初始值设定项,它接受 类型的参数列表(坐标:CLLocationCoordinate2D,addressDictionary:[String: String?])
我已经看过 API 文档和一个又一个关于如何进行反向地理编码的演示 - 在给定纬度/经度位置的情况下获取地址。我需要做相反的事情。我假设这个问题已经解决了,因为 Apple 的 MapKit API
我有一个 mkmapview,我在上面放置了几个地标图钉,但是我无法让图钉在标注上显示正确的标题,似乎随机显示图钉集合中的标题 map 。有任何想法吗?代码如下: (void)viewDidLoad
在我的应用程序中,我正在创建自己的地标,我需要将街道编号与街道名称分开。但是我看不出如何使用地址字典初始化 MKPlacemark 以便在 subThoroughfare 属性中返回街道号码,因为没有
我是一名新的 iOS 开发者。 我想将 MKPlacemark 实例保存到 Realm,但我不知道如何保存它。要保存在 Realm 中,所有属性必须是原语、NSString、NSDate、NSData
我得到了 MKLocalSearch 的结果,它包括类似... { address = { formattedAddressLine = (
问题 我有一组地标信息(国家、城市等)和一对纬度/经度。我想用它来创建一个 MKPlacemark 对象。 讨论 看来这个类只能由创建 - (id)initWithCoordinate:(CLLoca
我正在尝试将街道地址添加到我的 UITableViewCell 的 textDetailLabel。我遇到的问题是如何为 Table View 中的每个 cell 捕获唯一的街道地址。 现在它正在获取
placemark = [[MKPlacemark alloc]initWithCoordinate:storedCoordinate addressDictionary:addressDict];
我正在尝试获取地址。到目前为止我所掌握的只是使用 获取坐标 CLLocation loc = new CLLocation(anno.Coordinate.Latitude, anno.Coordi
每当在我的 MapView 上单击 MKPlacemark 时,我都会尝试执行特定功能。此函数从地标中提取信息并将其显示在单独的 View 中。我试过用 func mapView(_ mapView:
我是一名优秀的程序员,十分优秀!