iOS地理位置(始终返回该位置位于英国伦敦)

时间:2015-06-29 13:39:21

标签: ios geolocation location cllocationmanager cllocation

我一直在寻找一个解决方案,可以让我用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

1 个答案:

答案 0 :(得分:1)

您需要移动此代码:

[locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges];

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

更新:

根据OP的要求......这里有一些基于OP提供的附加信息的位置服务信息。

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

  1. 设备中的GPS芯片
  2. 小区服务已连接到网络
  3. WiFi服务已连接到网络
  4. 如果不满足上述条件中的至少一个,则无法接收位置数据。当设备中没有GPS芯片时,网络连接(Cell Tower / WiFi)用于计算您的位置。为此,设备基于其连接的网络确定位置信息。这可以是蜂窝塔位置或知道IP地址位置和WiFi位置。这种方法不如GPS准确,但通常可以更快地返回位置数据并使用更少的电池电量。