【问题标题】:Use CLLocationManager and MKReverseGeocoder to get name of city使用 CLLocationManager 和 MKReverseGeocoder 获取城市名称
【发布时间】:2011-03-20 19:01:23
【问题描述】:

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

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

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

这是我的代码:

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

【问题讨论】:

    标签: objective-c mkmapview cllocationmanager mkreversegeocoder


    【解决方案1】:

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

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

    • viewDidLoad:
      创建CLLocationManager 实例并开始更新位置 - 获取当前用户坐标

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

    • locationManager:didFailWithError: 和 reverseGeocoder:didFailWithError:
      处理可能的错误 - 只需将它们记录在当前实现中

    • reverseGeocoder:didFindPlacemark:
      当MKReverseGeocoder 找到您的坐标信息时被调用,您可以从您获得的MKPlacemark 实例的相应字段中检索您需要的信息。

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

    【讨论】:

    • 非常感谢!!!!我从博客的某个地方获得了这段代码,并不确定应该做什么。你能解释一下吗?提前致谢。
    • 另外,如果您能理解,请告诉我 kABPersonAddressCityKey 的意义是什么,因为实际上它没有被声明,我添加了 NSString *kABPersonAddressCityKey 行。非常感谢,谢谢,
    • 非常感谢!但是除了显示地图并显示我使用界面生成器所做的当前位置之外,这段代码仍然没有做任何事情。所以我想知道这段代码的意义是什么?它应该做什么。
    • 它在 didFindPlacemark 方法中接收地标,但什么也不做。您可以在那里添加日志记录以查看一些结果(以及它们是否有效)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    相关资源
    最近更新 更多