问题
I have a Mac app and would like to use core location, however, when I am not on wifi but connected using an ethernet cable, core location (CLLocationManager) reports that the operation could not be completed.
The exact error message is
The operation couldn't be completed. (kCLErrorDomain error 0.)
If I am always connected to the same router (ie. either wifi or ethernet cable) why does CLLocationManager only work for wifi and not for the ethernet connection?
Any suggestions would greatly be appreciated.
Thanks.
Edit:
Here is some code.
I define my location manager as an instance variable like so
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:ICMinimumUpdateDistance];
I then monitor the location manager's delegate method like so,
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// Filter out points before the last update
NSTimeInterval timeSinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:dateOfLastUpdate];
if (timeSinceLastUpdate > 0)
{
//Do stuff
}
}
I also check for errors using the delegate method
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Location Error:%@", [error localizedDescription]);
}
In the code above, the location manager updates with an invalid newLocation (bad time stamp) and then the location manager calls the delegate error method.
回答1:
I've noticed this too. If you open the Time Zone tab of the Date & Time pane in System Preferences while connected to the internet via ethernet, it says to connect to a wireless network to determine your current location. This leads me to believe that CoreLocation does, in fact, require a wireless connection.
回答2:
For lack of a GPS in your laptop, core-location on OSX uses the (skyhook) service, or something similar.
The service maintains a database of WIFI access-points and their positions (possibly updated by iPhones that do have GPS and wifi enabled) which is queried.
So by feeding a list of access points you can see, and their relative signal strengths the system is able to triangulate roughly where you are.
So you need both wifi enabled, and a working internet link (but internet shouldn't need to be over wifi, you can leave the airport un-associated)
来源:https://stackoverflow.com/questions/5492439/cllocationmanager-does-not-work-for-non-wireless-connection