首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CLLocationManager和MKReverseGeocoder获取城市名称

使用CLLocationManager和MKReverseGeocoder获取城市名称
EN

Stack Overflow用户
提问于 2010-07-26 00:40:50
回答 1查看 7.1K关注 0票数 2

我正在尝试使用MKReverseGeoCoder获取用户当前所在城市的名称,但它有一些我无法识别的错误。以下是详细信息:

它有一些我无法识别的错误

代码语言:javascript
复制
Undefined symbols:
  ".objc_class_name_CLLocationManager", referenced from:
      literal-pointer@__OBJC@__cls_refs@CLLocationManager in mapViewController.o
  "_kCLLocationAccuracyNearestTenMeters", referenced from:
      _kCLLocationAccuracyNearestTenMeters$non_lazy_ptr in mapViewController.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

下面是我的代码:

代码语言:javascript
复制
mapViewController.m

//
//  mapViewController.m
//  map
//
//  Created by Ashutosh Tiwari on 7/23/10.
//  Copyright ISU 2010. All rights reserved.
//
#import <MapKit/MapKit.h>
#import "mapViewController.h"

@implementation mapViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];

    [super viewDidLoad];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{

    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    geoCoder.delegate = self;
    [geoCoder start];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
    NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    MKPlacemark * myPlacemark = placemark;

    NSString *kABPersonAddressCityKey;
    NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
}

- (void)dealloc {
    [super dealloc];
}

@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-26 00:45:43

添加链接到项目的CoreLocation.framework (目标设置/链接库/添加/选择CoreLocation.framework)

添加:简要说明每个方法的作用:

  • viewDidLoad:

创建位置实例并开始更新位置-以获取当前用户coordinates

  • locationManager:didUpdateToLocation:

当CLLocationManager接收到用户坐标时调用。现在,我们可以将它们传递给MKReverseGeocoder以获取用户位置的信息(国家、城市etc)

  • locationManager:didFailWithError:reverseGeocoder:didFailWithError:

处理可能的错误-只需将它们记录在当前implementation

  • reverseGeocoder:didFindPlacemark:中

MKReverseGeocoder找到坐标的信息时调用,您可以从获取的MKPlacemark实例的相应字段中检索所需的信息。

kABPersonAddressCityKey -是placemark地址字典中城市字段的关键字符串。它在ABPerson.h报头中定义,因为placemark和ABRecord地址的地址字段是相同的。因此,要使用该密钥,您可能还需要链接到AddressBook框架。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3330054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档