使用 CLLocationManager 和 MKReverseGeocoder 获取城市名称

发布于 2024-09-11 04:28:34 字数 2115 浏览 7 评论 0原文

我正在尝试使用 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

I am trying to get the name of the city of user's current location by using MKReverseGeoCoder but it has some errors which I cannot recognize. Here are the details:

It has some errors which I cannot recognize

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

Here;s my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红玫瑰 2024-09-18 04:28:35

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

添加:简要介绍每种方法的作用:

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

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

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

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

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

Add CoreLocation.framework to link to your project (target settings/ Linked libraries / add / choose CoreLocation.framework)

Add: Briefly what each method does:

  • viewDidLoad:
    Creates CLLocationManager instance and starts updating location - to get current user coordinates

  • locationManager:didUpdateToLocation:
    Gets called when CLLocationManager receives user coordinates. Now we can pass them to MKReverseGeocoder to get information of user location (country, city etc)

  • locationManager:didFailWithError: and reverseGeocoder:didFailWithError:
    Handle possible errors - just log them in current implementation

  • reverseGeocoder:didFindPlacemark:
    Gets called when MKReverseGeocoder finds info for your coordinate, you can retrieve info you need from corresponding fields of MKPlacemark instance you get.

kABPersonAddressCityKey - is a key string for City field in placemark address dictionary. It is defined in ABPerson.h header because address fields for placemark and ABRecord address are the same. So to use that key you may need to link with AddressBook framework as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文