Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用iOS MapKit获取用户位置_Ios_Objective C_Xcode6_Mapkit - Fatal编程技术网

无法使用iOS MapKit获取用户位置

无法使用iOS MapKit获取用户位置,ios,objective-c,xcode6,mapkit,Ios,Objective C,Xcode6,Mapkit,让用户位置正常工作让我很痛苦 我读过很多文章,解释iOS 8的新变化,以及在info.plist文件中必须有两个键NSLocationWhenUsageDescription或NSLocationAlwaysUsageDescription中的一个,应用程序才能请求获得用户位置的权限。我做到了 我还包括了我认为位置服务工作所需的所有代码……但它没有。请参阅下面的my mapViewController.h和mapViewController.m文件: mapViewController.h 此外

让用户位置正常工作让我很痛苦

我读过很多文章,解释iOS 8的新变化,以及在info.plist文件中必须有两个键NSLocationWhenUsageDescription或NSLocationAlwaysUsageDescription中的一个,应用程序才能请求获得用户位置的权限。我做到了

我还包括了我认为位置服务工作所需的所有代码……但它没有。请参阅下面的my mapViewController.h和mapViewController.m文件:

mapViewController.h

此外,如果这是相关的,mapViewController将绑定到选项卡视图控制器中的选项卡。当我单击包含包含MKMapView的视图控制器的选项卡时,我会看到下图:


使用此代码并在设备中运行应用程序以获取当前位置。 如果您在模拟器中运行,那么它将显示默认位置,即苹果公司的地址作为当前位置

- (void)viewDidLoad
{ 
  [super viewDidLoad];
  self.locationManager = [[CLLocationManager alloc] init];
  [self.locationManager setDelegate:self ];
  self.mapView.showsUserLocation=YES;
  [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
  [self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];
  MKCoordinateRegion   region=MKCoordinateRegionMakeWithDistance(newLocation.coordinate,70000 ,70000 );
  MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
  [self.mapView setRegion:adjustedRegion animated:YES];
}
注:
打开设备中的数据或wifi,否则您将面临位置准确性错误。

将以下代码添加到您的ViewController.h类中

@interface ViewController : UIViewController<CLLocationManagerDelegate>

@property(nonatomic, retain) CLLocationManager *locationManager;
@end
根据文档,[self.locationManager requestAlwaysAuthorization]是异步的。实现location services auth changed委托回调,并仅在允许auth状态后才在那里开始监视

- (void)viewDidLoad
{ 
  [super viewDidLoad];
  self.locationManager = [[CLLocationManager alloc] init];
  [self.locationManager setDelegate:self ];
  self.mapView.showsUserLocation=YES;
  [self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
  [self.mapView setCenterCoordinate:newLocation.coordinate animated:YES];
  MKCoordinateRegion   region=MKCoordinateRegionMakeWithDistance(newLocation.coordinate,70000 ,70000 );
  MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
  [self.mapView setRegion:adjustedRegion animated:YES];
}
@interface ViewController : UIViewController<CLLocationManagerDelegate>

@property(nonatomic, retain) CLLocationManager *locationManager;
@end
-(void)viewWillAppear:(BOOL)animated{
self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
[super viewWillAppear:animated];



}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    CLLocationCoordinate2D zoomLocation = CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}