【问题标题】:iOS Geolocation (Always returning that location is in London UK)iOS 地理位置(始终返回该位置在英国伦敦)
【发布时间】:2015-06-29 13:39:21
【问题描述】:

多年来,我一直在寻找一种解决方案,可以让我通过 Wi-Fi 显示我的设备位置,但实际上并没有连接到任何网络。所以它只是打开了 Wi-Fi,但是我尝试过的每种方法都会返回我位于伦敦的信息。有谁知道为什么会这样?

我正在使用的方法

- (void)viewDidLoad
{
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    locationManager.distanceFilter = 1000;

#ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER) {
        // Use one or the other, not both. Depending on what you put in info.plist
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];
    }
#endif

    if ([self shouldFetchUserLocation])
    {
        [locationManager startUpdatingLocation];
    }

    // Do any additional setup after loading the view.
}

-(BOOL)shouldFetchUserLocation
{

BOOL shouldFetchLocation= NO;

if ([CLLocationManager locationServicesEnabled])
{
    switch ([CLLocationManager authorizationStatus])
    {
        case kCLAuthorizationStatusAuthorized:
            shouldFetchLocation= YES;
            break;
        case kCLAuthorizationStatusDenied:
        {
            UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"App level settings has been denied" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];
            alert= nil;
        }
            break;
        case kCLAuthorizationStatusNotDetermined:
        {
            UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The user is yet to provide the permission" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];
            alert= nil;
        }
            break;
        case kCLAuthorizationStatusRestricted:
        {
            UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The app is recstricted from using location services." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];
            alert= nil;
        }
            break;

        default:
            break;
    }
}
else{
    UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error" message:@"The location services seems to be disabled from the settings." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
    alert= nil;
}

return shouldFetchLocation;
}

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

    NSLog(@"location fetched in delegate");

    CLLocation* location = [locations lastObject];
    NSDate* eventDate = location.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

    if (fabs(howRecent) < 15.0) {
        // If the event is recent, do something with it.
        NSLog(@"inside loop.... latitude %+.6f, longitude %+.6f\n",
              location.coordinate.latitude,
              location.coordinate.longitude);
    }
    NSLog(@"latitude %+.6f, longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);
    NSLog(@"\nlatitude: %+.6f \nlongitude:   %+.6f", location.coordinate.latitude, location.coordinate.longitude);

    [locationManager stopUpdatingLocation];
    [locationManager stopMonitoringSignificantLocationChanges];

    if(locationManager!=nil){
        locationManager.delegate= nil;
        locationManager= nil;
    }
}

输出:

(不带项目符号的输出)

  • 在委托中获取位置
  • 内环...纬度+51.509980,经度-0.133700
  • 纬度+51.509980,经度-0.133700
  • 纬度:+51.509980
  • 经度:-0.133700

【问题讨论】:

  • 愚蠢的问题,但是......你有机会在伦敦吗? :)
  • @simpleBob Nope :P 我是个老北方人 :) 我现在在曼彻斯特
  • 输出是什么?是否发出任何警报? startUpdatingLocation 甚至被调用了吗?
  • 我已经更新了我的问题以显示我得到的结果
  • 有时 at 需要一点时间才能找到正确的位置。您是否尝试过让更新活动保持更长时间?

标签: ios geolocation location cllocationmanager cllocation


【解决方案1】:

你需要移动这段代码:

[locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges];

返回一个有效位置后调用的另一个函数。现在你只让locationManage 更新你的位置一次,这很可能是一个缓存位置(伦敦?)。在返回有效位置之前,通常需要多次调用 didUpdateLocations 委托。

更新:

根据 OP 的要求...这里是基于 OP 提供的附加信息的有关位置服务的更多信息。

如果您的设备没有 GPS 并且您没有连接到网络(手机/WiFi),那么您的要求是不可能的。要接收位置数据,您至少需要以下一项:

  1. 您设备中的 GPS 芯片
  2. 蜂窝服务已连接到网络
  3. WiFi 服务已连接到网络

如果不满足上述条件中的至少一项,您将无法接收位置数据。当设备中没有 GPS 芯片时,网络连接(手机信号塔/WiFi)将用于计算您的位置。为此,设备根据其连接的网络确定位置信息。这可以是手机信号塔位置或知道 IP 地址位置和 WiFi 位置。这种方法不如 GPS 准确,但通常可以更快地返回位置数据并且使用更少的电池电量。

【讨论】:

  • 这似乎不起作用,是因为我使用的是没有 GPS 芯片和 3G 的 iPad 吗?
  • 好的,所以你的 iPad 没有 GPS?如果是这种情况,那么您所要求的是不可能的。要接收位置数据,您需要在设备中安装 GPS 芯片或将手机和/或 WiFi 连接到网络。如果您的 Cell/WiFi 未连接到网络,您将无法接收位置数据。网络连接(手机信号塔/IP 地址)用于在没有 GPS 芯片时计算您的位置。
  • 谢谢你的解释,你能把它放在你的答案中并解释一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 2011-03-19
  • 2012-07-30
  • 2023-03-11
  • 2017-03-01
相关资源
最近更新 更多