【问题标题】:How to get the current location or GPS coordinates with Core Location?如何通过核心位置获取当前位置或 GPS 坐标?
【发布时间】:2010-06-24 17:58:14
【问题描述】:

如何使用 Core Location 框架获取当前位置或 GPS 坐标?

【问题讨论】:

    标签: iphone coordinates core-location


    【解决方案1】:

    首先,您需要一个实现协议CLLocationManagerDelegate 的类。所以至少你需要实现方法:

    - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation
    {
     ...
    }
    

    创建 CLLocationManager 实例后,设置委托并开始更新位置:

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate        = self;  //SET YOUR DELEGATE HERE
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; //SET THIS TO SPECIFY THE ACCURACY
    [locationManager startUpdatingLocation];
    

    调用startUpdatingLocation 后,一旦发生位置修复,您的locationManager: didUpdateToLocation: fromLocation 实现就会被调用。参数newLocation 包含您的实际位置。但是即使没有给出您指定的准确性,位置管理器也会尽快修复一个位置。对于这种情况,您必须自己检查新位置的准确性。

    干杯, 安卡

    【讨论】:

    • 嗨,在locationManager 方法中,我可以将经度/纬度存储到变量中以供以后在应用程序中使用吗?像这样:- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { float long=newLocation.longitude; float lat=newLocation.latitude; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2012-05-18
    • 2016-09-19
    相关资源
    最近更新 更多