Για να πάρετε την τοποθεσία του χρήστη, θα πρέπει πραγματικά να «ξεκινήσει» με τον υπεύθυνο του θέση πριν προσπαθήσετε να διαβάσετε τις πληροφορίες τοποθεσίας. Προτείνω να εξετάσουμε LocateMe δείγμα από την Apple.
LocateMe Δείγμα κώδικα
Με λίγα λόγια, πληροφορίες για τον εντοπισμό πάρει χρήστη μπορεί να γίνει σε δύο στάδια:
- (void)viewDidLoad {
[[self locationManager] startUpdatingLocation];
}
- (CLLocationManager *)locationManager {
if (locationManager != nil) {
return locationManager;
}
NSLog(@"%s, Creating new Location Manager instance.", __FUNCTION__);
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = DISTANCE_FILTER_VALUE;
locationManager.delegate = self;
return locationManager;
}
... και μετά,
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"%s, location manager returned a new location.", __FUNCTION__);
// Check if location is valid (or good enough for us),
// and then process the location
if ([self locationIsValid:newLocation]) {
[self processLocation:newLocation];
// Very important! When your done with it, stop the location manager
[[self locationManager] sopUpdatingLocation];
// (Maybe even) clear it
locationManager.delegate = nil;
[locationManager release];
locationManager = nil;
}
}
Η ελπίδα αυτό βοηθά!