iOS 地理位置(返回的位置总是在英国伦敦)
iOS Geolocation (Always returning that location is in London UK)
多年来,我一直在四处寻找解决方案,让我可以通过 Wi-Fi 显示我的设备位置,但实际上并未连接到任何网络。所以它只是打开了 Wi-Fi,但是我尝试过的每一种方法 returns 我都在伦敦。有谁知道为什么会这样?
我正在使用的方法
- (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
您需要移动此代码:
[locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges];
到您拥有有效位置后调用的另一个函数 returned。现在你只让 locationManage
更新你的位置一次,这很可能是一个缓存位置(伦敦?)。通常需要多次调用 didUpdateLocations
委托才能 returned 一个有效位置。
更新:
根据 OP 的要求...这里是根据 OP 提供的附加信息提供的有关定位服务的更多信息。
如果您的设备没有 GPS 并且您没有连接到网络 (cell/WiFi),那么您所要求的是不可能的。要接收位置数据,您至少需要以下其中一项:
- 您设备中的 GPS 芯片
- 手机服务已连接到网络
- WiFi 服务已连接到网络
如果至少不满足上述条件之一,您将无法接收位置数据。如果设备中没有 GPS 芯片,则会使用网络连接 (Cell Tower/WiFi) 来计算您的位置。为此,设备会根据其连接的网络确定位置信息。这可以是手机信号塔位置或已知 IP 地址位置和 WiFi 位置。此方法不如 GPS 准确,但通常可以更快地获取 return 位置数据并且使用更少的电池电量。
多年来,我一直在四处寻找解决方案,让我可以通过 Wi-Fi 显示我的设备位置,但实际上并未连接到任何网络。所以它只是打开了 Wi-Fi,但是我尝试过的每一种方法 returns 我都在伦敦。有谁知道为什么会这样?
我正在使用的方法
- (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
您需要移动此代码:
[locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges];
到您拥有有效位置后调用的另一个函数 returned。现在你只让 locationManage
更新你的位置一次,这很可能是一个缓存位置(伦敦?)。通常需要多次调用 didUpdateLocations
委托才能 returned 一个有效位置。
更新:
根据 OP 的要求...这里是根据 OP 提供的附加信息提供的有关定位服务的更多信息。
如果您的设备没有 GPS 并且您没有连接到网络 (cell/WiFi),那么您所要求的是不可能的。要接收位置数据,您至少需要以下其中一项:
- 您设备中的 GPS 芯片
- 手机服务已连接到网络
- WiFi 服务已连接到网络
如果至少不满足上述条件之一,您将无法接收位置数据。如果设备中没有 GPS 芯片,则会使用网络连接 (Cell Tower/WiFi) 来计算您的位置。为此,设备会根据其连接的网络确定位置信息。这可以是手机信号塔位置或已知 IP 地址位置和 WiFi 位置。此方法不如 GPS 准确,但通常可以更快地获取 return 位置数据并且使用更少的电池电量。