【发布时间】: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