如何在MacOS上检索位置信息?

时间:2012-04-17 14:56:59

标签: macos geolocation location core-location cllocationmanager

我偶然发现了 CoreLocation 框架并实现了iOS Location Awareness Programming Guide 中描述的基本步骤,因为我找不到OSX。

// AppDelegate.h

#import <Cocoa/Cocoa.h>
#import <CoreLocation/CoreLocation.h>

@interface AppDelegate : NSObject<NSApplicationDelegate, CLLocationManagerDelegate> {
    CLLocationManager* m_locationManager;
}
@end

实施文件中没有那么多......

// AppDelegate.m

- (void)applicationDidFinishLaunching:(NSNotification*)notification {
    m_locationManager = [[CLLocationManager alloc] init];
    m_locationManager.delegate = self;
    m_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [m_locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    NSLog(@"%@ %@", self.className, NSStringFromSelector(_cmd));
}

- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation {
    NSLog(@"%@ %@", self.className, NSStringFromSelector(_cmd));
}

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

当我启动应用程序时,我被要求授权位置查找。一段时间后再次出现同样的问题 问题:但是,没有调用任何委托方法。


BTW:一篇关于Apple and Google Maps宣布CLGeocoder的有趣文章。

2 个答案:

答案 0 :(得分:2)

使用此代理

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

答案 1 :(得分:0)

我能想到的两件事:

1)

你的应用程序是否可以在MacOS 10.6&amp;新?如果是这样,请确保项目的“部署目标”设置设置为10.6或更高版本。

2)

在“.h”接口文件中,确保添加<CLLocationManagerDelegate>以指示此对象符合协议。看起来您正在尝试从应用程序委托中执行此操作,因此声明可能类似于:

@interface AppDelegate : NSObject <NSApplicationDelegate, CLLocationManagerDelegate>