gpt4 book ai didi

ios - 未调用 CLLocation Manager 委托(delegate)?

转载 作者:行者123 更新时间:2023-11-29 11:47:50 25 4
gpt4 key购买 nike

我制作了一个获取当前位置的演示。我为当前位置使用了 .GPX 文件。我使用了 CLLocation 方法的委托(delegate)方法。

我还在我的 plist 文件中使用了键“隐私 - 使用时的位置使用说明”。

我知道这个问题已经问了很多次了,但是得不到结果。

代码是

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

帮帮我,伙计们无法获得位置。因为委托(delegate)方法没有调用。

每次它调用这个方法。

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

最佳答案

您需要检查位置权限。然后您可以调用委托(delegate)方法。

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

关于ios - 未调用 CLLocation Manager 委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42694464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com