【问题标题】:How to Get only the device GPS location ios如何仅获取设备 GPS 位置 ios
【发布时间】:2015-11-02 02:46:22
【问题描述】:

有什么方法可以使用 ios 中的 corelocation 获取唯一的设备 gps 位置。 目前我正在使用以下代码。

- (id)init{
if (!(self = [super init]))
    return nil;

//Setup the manager
manager = [[CLLocationManager alloc] init];
if (!manager) {
    return nil;
}
manager.distanceFilter = kCLDistanceFilterNone;
manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
manager.desiredAccuracy = kCLLocationAccuracyBest;
manager.delegate = self;
if ([manager respondsToSelector:@selector(pausesLocationUpdatesAutomatically)]) {
    manager.pausesLocationUpdatesAutomatically = NO;
}
if ([manager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
    [manager requestAlwaysAuthorization];
}

[manager startUpdatingLocation];

return self;
}

【问题讨论】:

  • 什么意思?您只想要没有 Wifi 和蜂窝精度的实际 GPS 坐标?
  • 是的,如果设备不支持 wifi 或蜂窝,我只想要 gps 坐标。
  • CoreLocation 将为您提供根据 GPS、Wifi 和蜂窝网络计算的位置。您不能要求它仅使用特定的定位方法来计算位置。位置计算方法对你来说完全是黑暗的,你不应该关心它。
  • 我已经检查了我的 plist 有两个我已经添加在那里的字符串。当没有互联网连接可用时,我的设备会为我提供位置。但我只想要设备位置,无论是否连接互联网。

标签: ios gps core-location


【解决方案1】:

您应该将此代码添加到您的文件中。它在收到新位置时执行:

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
      didUpdateLocations:(NSArray *)locations {
    // If it's a relatively recent event, turn off updates to save power.
   CLLocation* location = [locations lastObject];
   NSDate* eventDate = location.timestamp;
   NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
   if (abs(howRecent) < 15.0) {
      // If the event is recent, do something with it.
      NSLog(@"latitude %+.6f, longitude %+.6f\n",
              location.coordinate.latitude,
              location.coordinate.longitude);
   }

}

源代码:Getting the Users Location - Apple

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    相关资源
    最近更新 更多