【问题标题】:How to retrieve location information on MacOS?如何在 MacOS 上检索位置信息?
【发布时间】:2012-04-28 22:28:24
【问题描述】:

我偶然发现了 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));
}

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


顺便说一句:一篇关于 Apple and Google Maps 宣布 CLGeocoder 的有趣文章。

【问题讨论】:

    标签: macos geolocation location core-location cllocationmanager


    【解决方案1】:

    使用这个委托

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

    【讨论】:

      【解决方案2】:

      我能想到的两件事:

      1)

      您的应用会在 MacOS 10.6 及更高版本上运行吗?如果是这样,请确保您的项目的“部署目标”设置为 10.6 或更高版本。

      2)

      在您的“.h”接口文件中,确保添加&lt;CLLocationManagerDelegate&gt; 以表明该对象符合协议。看起来您正试图从您的应用程序委托中执行此操作,因此声明可能类似于:

      @interface AppDelegate : NSObject <NSApplicationDelegate, CLLocationManagerDelegate>
      

      【讨论】:

      • Mac OS X 部署目标 设置为 Mac OS X 10.7 并在头文件中设置委托。
      • 你在什么样的机器上运行这个?我不确定 Mac 是否有任何可以使用的定位硬件。
      • @rdelmar 这是一台运行 Lion 的 Mac Pro。据我了解,位置 API 封装了 WIFI、GPS 等,所以我在什么样的机器上运行它没有区别。
      • 好吧,也许我尝试过的代码不正确,但我一直无法在 iMac 上获取位置信息。我想我在某处(几年前)读到,您的路由器必须注册某些服务才能提供位置信息。
      猜你喜欢
      • 1970-01-01
      • 2022-12-20
      • 1970-01-01
      • 2020-09-04
      • 2012-02-05
      • 2011-01-13
      • 1970-01-01
      • 2017-03-11
      • 2021-08-24
      相关资源
      最近更新 更多