2016-03-24 4 views
1

Ich versuche, aktuellen Benutzerstandort mit zu erhalten, aber aus irgendeinem Grund wird die Delegate-Methode locationManager: didUpdateLocations: nicht aufgerufen.CLLocationManager requestLocation ruft nicht auf didUpdateLocations

Ich rufe requestLocation von einem UIButton (IBOutlet) Handler.

Irgendeine Idee warum?

Vielen Dank!

+0

Haben Sie 'NSLocationWhenInUseUsageDescription' und' NSLocationAlwaysUsageDescription' in Ihrer Datei 'Info.plist' festgelegt? – Jelly

+0

Nur 'NSLocationAlwaysUsageDescription' –

+0

Versuchen Sie, die andere auch zu setzen. – Jelly

Antwort

0

Rechtsklick auf info.plist offen -> Quellcode fügen Sie den folgenden Code in <dict>...</dict> <key>NSLocationAlwaysUsageDescription</key> <string>App would like to use your location.</string> <key>NSLocationUsageDescription</key> <string>I need Location</string> <key>NSLocationWhenInUseUsageDescription</key> <string>App would like to use your location.</string> <key>UIMainStoryboardFile</key> Import

#import <CoreLocation/CoreLocation.h> 
#import <MobileCoreServices/MobileCoreServices.h> 

Dann in viewcontroller.h CLLocationManagerDelegate in viewcontroller.m Datei hinzufügen dieser Methode hinzufügen .

-(void)getCurrentLocation{ 
self.locationManager = [[CLLocationManager alloc] init]; 
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
    [self.locationManager requestWhenInUseAuthorization]; 
    [self.locationManager requestAlwaysAuthorization]; 
} 
self.locationManager.delegate = self; 
self.locationManager.distanceFilter = 10; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[self.locationManager startUpdatingLocation]; 
} 

Dann rufen Sie die CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manage 
didUpdateLocations:(NSArray *)locations 
{ 
CLLocation *currentAction = [locations objectAtIndex:0]; 
CLLocation *crnLoc = [locations lastObject]; 

[self.locationManager stopUpdatingLocation]; 
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ; 

CLLocation* location = [locations lastObject]; 
NSDate* eventDate = location.timestamp; 
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 
if (abs(howRecent) < 15.0) { 
    // If the event is recent, do something with it. 
    NSLog(@"latitude~~~: %+.8f, longitude~~~: %+.8f\n", 
      location.coordinate.latitude, 
      location.coordinate.longitude); 
    self.latitudeLabel.text=[NSString stringWithFormat:@"%.8f", 
    crnLoc.coordinate.latitude]; 
    self.longitudeLabel.text=[NSString stringWithFormat:@"%.8f", 
     crnLoc.coordinate.longitude]; 
} 
self.latitudeLabel.text=[NSString stringWithFormat:@"%.8f", 
crnLoc.coordinate.latitude]; 
self.longitudeLabel.text=[NSString stringWithFormat:@"%.8f", 
crnLoc.coordinate.longitude]; 


[geocoder reverseGeocodeLocation:currentAction 
completionHandler:^(NSArray *placemarks, NSError *error) 
{ 
    if (!(error)) 
    { 
     CLPlacemark *placemark = [placemarks objectAtIndex:0]; 

     NSString *locatedAt = [[placemark.addressDictionary 
    valueForKey:@"FormattedAddressLines"] 
    componentsJoinedByString:@", "]; 
    NSString *Address = [[NSString alloc]initWithString:locatedAt]; 
     NSLog(@"Adress %@",Address); 
    } 
    else 
    { 
     NSLog(@"\n Current Location Not Detected \n"); 
     return; 
    } 
}]; 
} 

Hoffnung diesen Code Hilfe Sie.

0

Ich denke, dass Ihr Problem mit this answer zusammenhängt.

Wenn Sie Ihre CLLocationManager Instanz als Instanz auf Funktionsebene erstellen, wird sie zu irgendeinem Zeitpunkt nach Beendigung der Funktion (wahrscheinlich vor dem Aufruf des Delegaten) deocokiert. Wenn Sie es stattdessen als Instanz auf Klassenebene erstellen, wird der Delegat aufgerufen, da die Instanz CLLocationManager weiterhin existiert.

Verwandte Themen