2016-03-24 9 views
1

[self.locationManager requestLocation]で現在のユーザーの場所を取得しようとしていますが、何らかの理由でデリゲートメソッドlocationManager: didUpdateLocations:が呼び出されていません。CLLocationManager requestLocationがdidUpdateLocationsを呼び出さない

UIButtonIBOutlet)ハンドラからrequestLocationを呼び出しています。

理由は何ですか?

ありがとうございました!右のようにInfo.plistのオープンをクリックして

+0

あなたの 'Info.plist'ファイルに' NSLocationWhenInUseUsageDescription'と 'NSLocationAlwaysUsageDescription'を設定しましたか? – Jelly

+0

ちょうど 'NSLocationAlwaysUsageDescription' –

+0

もう1つも設定してみてください。 – Jelly

答えて

0

- >ソースコードはviewcontroller.hに続いて<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 <CoreLocation/CoreLocation.h> 
#import <MobileCoreServices/MobileCoreServices.h> 

に次のコードを追加し、このメソッドを追加viewcontroller.mファイルにCLLocationManagerDelegate を追加。

-(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]; 
} 

その後

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; 
    } 
}]; 
} 
を呼び出すには、このコードのヘルプあなたを願っています。

0

あなたの問題はthis answerに関連していると思います。

CLLocationManagerインスタンスを関数レベルのインスタンスとして作成している場合、関数が終了した後のある時点(おそらく、デリゲートが呼び出される前)で取り消されます。代わりにクラスレベルのインスタンスとして作成する場合は、CLLocationManagerインスタンスが存在するため、デリゲートが呼び出されます。

関連する問題