I've been looking around for ages to find a solution that will allow me to show my device location with Wi-Fi but not actually connected to any network. So it just has Wi-Fi turned on, however every method I've tried returns me with that I'm located in London. Does anyone know why this is happening?
Method i'm using
- (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;
}
}
Output:
(Output without the bullet points)
You need to move this code:
[locationManager stopUpdatingLocation];
[locationManager stopMonitoringSignificantLocationChanges];
To another function that you call once your have a valid location returned. Right now you are only letting the locationManage
update your location once, and this will most likely be a cached location (London?). It usually takes several calls to the didUpdateLocations
delegate before a valid location is returned.
UPDATE:
Per OP's request... here is a little more info about location services based on the additional info provided by OP.
If your device does NOT have GPS AND you are not connected to a network (cell/WiFi) then what you are asking for is not possible. To receive location data, you need at least one of the following:
You cannot receive location data if at least one of the above conditions are not met. When a GPS chip is not present in the device, the network connection (Cell Tower/WiFi) is used to calculate your location instead. To do this the device determines location information based on the network it is connected to. This can be cell tower locations or know IP address locations and WiFi locations. This method is not as accurate as GPS but it is usually much quicker to return location data and uses less battery power.