使用iPhone GPS无需互联网更新用户位置 [英] Updating user location using iPhone GPS without internet

查看:140
本文介绍了使用iPhone GPS无需互联网更新用户位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

   - (void)updatestart 
{
//当前位置
_locationManager = [[CLLocationManager alloc]在里面];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];

$ b - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@didFailWithError:%@,错误);
UIAlertView * errorAlert = [[UIAlertView alloc]
initWithTitle:@错误消息:@无法获取您的位置委托:无cancelButtonTitle:@OKotherButtonTitles:nil];
[errorAlert show]; (CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {

[_locationManager stopUpdatingLocation];

NSLog(@%f,_ locationManager.location.coordinate.latitude);
NSLog(@%f,_ locationManager.location.coordinate.longitude);
}

我正在获取位置更新,但这只适用于我们有互联网连接。

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



任何有关如何实现这一点



预先感谢。

解决方案

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

  • 如果您没有获得位置,则需要很长时间才能获得位置(由于卫星搜索,这是

  • 它不适用于建筑物内或建筑物之间的街道太小
    (这种情况很多在意大利)

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



    从你的代码中,我发现应该尽快修复三件事。




    • 有时第一个位置被缓存,并不代表
      的实际位置

    • 它会更好当您收到
      有效坐标时停止位置管理器,这意味着:未缓存,水平精度> = 0且水平精度符合您的要求

    • 获取位置的委托方法已被弃用(取决于您的
      部署目标)。以下是前两个
      点的小片段:


         - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

      CLLocation * newLocation = [locations lastObject];
      if(newLocation.horizo​​ntalAccuracy <0){
      return;
      }
      NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];
      if(abs(interval)> 20){
      return;
      }
      }



    I need to get user location and fetch latitude and longitude of even when there is no internet available.

    Right now i have implemented CoreLocation methods:-

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

    and i am getting the location updates but this only works if we have internet connection.

    I guess using iPhone GPS we can fetch the location even without internet.

    Any idea of how to implement that??

    Thanks in advance.

    解决方案

    GPS doesn't need data exchange using internet, but it has basically 2 disadvantages:

    1. it takes a long time to get position if you haven't used it recently (this is due to satellite search)
    2. it doesn't work inside buildings or where streets are too small between buildings (this happens a lot in Italy)

    Another way that it doesn't need data exchange is location based on cell tower, but of course your device should have cellular chip installed.

    From your code I see three things that should be fixed as soon as possible.

    • Sometimes the first location is cached and it doesn't represent the actual location
    • It will be better to stop the location manager when you receive a valid coordinate, that means: not cached, with an horizontal accuracy >=0 and with an horizontal accuracy that match your requirements,
    • The delegate methods to get location is deprecated (depending on your deployment target). Here is a little snippet for the first two points:

      -(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 GPS无需互联网更新用户位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆