iphone - 在没有互联网的情况下使用 iPhone GPS 更新用户位置

标签 iphone ios objective-c geolocation

即使没有可用的互联网,我也需要获取用户位置并获取纬度和经度。

现在我已经实现了 CoreLocation 方法:-

    -(void)updatestart
    {
        // Current location
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.delegate = self;
        [_locationManager startUpdatingLocation];
    }

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation{

    [_locationManager stopUpdatingLocation];

    NSLog(@"%f",_locationManager.location.coordinate.latitude);
    NSLog(@"%f",_locationManager.location.coordinate.longitude);
}

我正在获取位置更新,但这仅在我们有互联网连接的情况下才有效。

我想即使没有互联网,我们也可以使用 iPhone GPS 获取位置。

知道如何实现吗?

提前致谢。

最佳答案

GPS不需要使用互联网进行数据交换,但它基本上有两个缺点:

  • 如果您最近没有使用它需要很长时间才能获得位置(这是
    由于卫星搜索)
  • 它不适用于建筑物内或街道太小的地方
    建筑物之间(这在意大利经常发生)

  • 它不需要数据交换的另一种方式是基于蜂窝塔的位置,但当然你的设备应该安装蜂窝芯片。

    从您的代码中,我看到应该尽快修复三件事。
  • 有时第一个位置被缓存,它并不代表
    实际位置
  • 当您收到一个位置管理器时最好停止
    有效坐标,即:未缓存,水平精度 >=0 且水平精度符合您的要求,
  • 获取位置的委托(delegate)方法已被弃用(取决于您的
    部署目标)。这是前两个的小片段
    要点:
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
        CLLocation * newLocation = [locations lastObject];
        if (newLocation.horizontalAccuracy < 0) {
            return;
        }
        NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];
        if (abs(interval)>20) {
            return;
        }
    }
    
  • 关于iphone - 在没有互联网的情况下使用 iPhone GPS 更新用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17269921/

    相关文章:

    objective-c - Objective-C : Memory Leak issue in Class Method

    ios - 从 NSMutableArray 中删除随机项

    iOS Sqlite 数据库不显示第一条记录

    ios - 通过在 ImageView 中显示的代码访问图像库

    ios - 根据 UILabel 内容调整 UITableViewCell 高度

    ios - 使用带有 audiokit 的 SF2 声音字体库将 MIDI 文件转换为音频文件

    iphone - iOS本地通知: I would like to execute a function upon reentering the app via local notification

    iphone - 对齐指针

    iphone - 推送通知-应用角标(Badge)图标

    iphone - 应用程序加载器 (Apple) 卡在 "Sending API usage to iTunes Connect"