【问题标题】:Updating user location using iPhone GPS without internet在没有互联网的情况下使用 iPhone GPS 更新用户位置
【发布时间】:2013-06-24 07:16:18
【问题描述】:

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

现在我已经实现了 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 接收器直接访问 GPS,你不需要任何互联网。但是,如果您在任何屋檐下,那么 GPS 接收器无法直接访问 GPS,那么它会使用您的互联网来定位您。
  • 你的意思是说通过使用上述相同的方法我可以使用 GPS 更新用户位置??

标签: iphone ios objective-c geolocation


【解决方案1】:

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

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

另一种不需要数据交换的方式是基于手机信号塔的位置,但您的设备当然应该安装手机芯片。

从您的代码中,我看到了应该尽快修复的三件事。

  • 有时第一个位置被缓存,它并不代表 实际位置
  • 最好在收到 有效坐标,这意味着:未缓存,水平精度 >=0 且水平精度符合您的要求,
  • 获取位置的委托方法已被弃用(取决于您的 部署目标)。这是前两个的小sn-p 分:

    -(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;
        }
    }
    

【讨论】:

  • 哦,我可以解决这个问题。但是,如果我没有互联网连接,那么它会立即调用 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 。在这种情况下该怎么办?
猜你喜欢
  • 2013-09-11
  • 1970-01-01
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多