代码之家  ›  专栏  ›  技术社区  ›  Ashutosh

使用CLLocationManager和MKReverseGeocoder获取城市名称

  •  2
  • Ashutosh  · 技术社区  · 15 年前

    我试图通过使用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
    
    1 回复  |  直到 12 年前
        1
  •  8
  •   Vladimir    15 年前

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

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

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

    • locationManager:didUpdateToLocation:
      当CLLocationManager接收到用户坐标时调用。现在我们可以把它们传给 MKReverseGeocoder

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


    • MKReverseGeocoder公司 查找坐标的信息,您可以从坐标的相应字段中检索所需的信息 MKPlacemark 你得到的实例。

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