2017-03-09 1 views
1

나는 현재 위치를 얻기위한 하나의 데모를 만들었습니다. 현재 위치에 .GPX 파일을 사용했습니다. CLLocation 메서드의 Delegates 메서드를 사용했습니다.CLLocation Manager Delegates Not Called?

필자의 plist 파일에서도 Key "Privacy - Location When In Use Usage Description"을 사용했습니다.

이 질문은 이미 많은 시간을 요구했지만 결과를 얻을 수 없다는 것을 알고 있습니다.

코드는 메소드가 호출되지 location.Because 위임을 얻을 수

#import "ViewController.h" 
#import <CoreLocation/CoreLocation.h> 
#import <MapKit/MapKit.h> 

@interface ViewController()<CLLocationManagerDelegate,MKMapViewDelegate> 

@property (nonatomic,assign)CLLocationCoordinate2D cordinateLocation; 
@property (nonatomic,strong)MKPointAnnotation *point; 
@property (nonatomic,weak)IBOutlet MKMapView *mapView; 

@end 

@implementation ViewController 
CLLocationManager *locationManager; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
locationManager = [[CLLocationManager alloc]init]; 
    locationManager.delegate = self; 
    self.mapView.delegate = self; 
    [self updateCurrentLocation]; 
} 

- (void)updateCurrentLocation { 

    locationManager = [[CLLocationManager alloc] init]; 

    locationManager.delegate = self; 

    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
     [locationManager requestWhenInUseAuthorization]; 
    } 

    [locationManager startUpdatingLocation]; 
} 

//-(void)getCurrentLocation 
//{ 
// [locationManager requestWhenInUseAuthorization]; 
// locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
// [locationManager startUpdatingHeading]; 
// [locationManager startUpdatingLocation]; 
// 
//} 
- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"didFailWithError: %@", error); 

    UIAlertController *errorAlert =[UIAlertController alertControllerWithTitle:@"Error" message:@"error reported" preferredStyle:UIAlertControllerStyleAlert]; 
    UIAlertAction *alertStyle = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; 

    [errorAlert addAction:alertStyle]; 

} 


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations 
{ 
    CLLocation *updatedLocation = [locations lastObject]; 
    if(updatedLocation != nil) 
    { 
     self.cordinateLocation=CLLocationCoordinate2DMake(updatedLocation.coordinate.latitude, updatedLocation.coordinate.longitude); 
     [manager stopUpdatingLocation]; 
      [self setCurrentLocationFocus]; 
    } 
    else{ 
     NSLog(@"Can't access desire location"); 
    } 
} 

-(void)setCurrentLocationFocus{ 

    MKCoordinateRegion region; 
    region.center = self.cordinateLocation; 
    //Adjust span as you like 
    MKCoordinateSpan span; 
    span.latitudeDelta = 1; 
    span.longitudeDelta = 1; 
    region.span = span; 
    [self.mapView setRegion:region animated:YES]; 

    ///Drop the pin on Current Locatio //// 
    self.point = [[MKPointAnnotation alloc] init]; 
    self.point.coordinate = self.cordinateLocation; 
    self.point.title = @"FlockStation"; 
    self.point.subtitle = @"It Department"; 
    [self.mapView addAnnotation:self.point]; 

    //set a new camera angle 
    MKMapCamera *newCamera=[[MKMapCamera alloc] init]; 
    [newCamera setCenterCoordinate:self.cordinateLocation]; 
    [newCamera setPitch:60.0]; ///For zooming purpose/// 
    [newCamera setHeading:90]; ///For Compass Purpose /// 
    [newCamera setAltitude:100.0]; ///On which height you want to see your map /// 
    [self.mapView setCamera:newCamera animated:YES]; 
} 

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    UIViewController *viewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"NewViewController"]; 
    [self presentViewController:viewController animated:YES completion:nil]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{ 
    // self.updatedHeading.text = [NSString stringWithFormat:@"%f",newHeading.magneticHeading]; 
} 

도움말 저 녀석이 아니다.

항상이 방법을 호출했습니다.

- (void)locationManager:(CLLocationManager *)manager didFailWithError:`(NSError *)error 
+0

시뮬레이터 또는 기기에서이 제품을 사용하려고하십니까? 시뮬레이터에있는 경우 ** 디버그> 위치> 고속도로 드라이브 **를 선택하여 시뮬레이터의 맞춤 위치를 활성화해야합니다. –

답변

1

위치의 허가를 확인해야합니다. 그런 다음 대리인 메서드를 호출 할 수 있습니다.

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    locationManager=[[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [locationManager startUpdatingLocation]; 

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 
    { 
     [locationManager requestWhenInUseAuthorization]; 
    } 
    if ([CLLocationManager locationServicesEnabled]){ 

     NSLog(@"Location Services Enabled"); 

     if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){ 
      alert = [[UIAlertView alloc] initWithTitle:@"Permission Denied" 
               message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
               delegate:nil 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 
      [alert show]; 
     } 
    } 
} 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    CLLocation *currentLocation = newLocation; 
    txtlocations.text = [NSString stringWithFormat:@"%f & %f",currentLocation.coordinate.latitude, currentLocation.coordinate.longitude]; 
} 
+0

: 이미 시도해 보았습니다. Not Working Thank – hd1344

+0

지금 확인하십시오.이 코드는 내 장치에서 완벽하게 작동합니다. – kalpesh

+0

Kalpesh 감사합니다. – hd1344

0

약간의 변경 및 작동으로 코드를 사용해 보았습니다.

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [self updateCurrentLocation]; 
} 

- (void)updateCurrentLocation { 

    if([CLLocationManager locationServicesEnabled] && 
     [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) { 
     locationManager = [[CLLocationManager alloc] init]; 
     locationManager.delegate=self; 
     locationManager.desiredAccuracy=kCLLocationAccuracyBest; 
     locationManager.distanceFilter=kCLDistanceFilterNone; 
     [locationManager requestWhenInUseAuthorization]; 
     [locationManager startMonitoringSignificantLocationChanges]; 
     [locationManager startUpdatingLocation]; 
     // show the map 
    } else { 
     // show error 



    } 
} 
관련 문제