【发布时间】:2015-01-15 07:37:33
【问题描述】:
我在这个网站、苹果开发网站和无数的 YouTube 视频上查看了其他类似的问题,但我仍然无法弄清楚这一点。在 Xcode/iOS8 中获取用户位置的信息似乎很少。
我有一个工作正常的地图视图,但当我单击按钮获取我的位置时,没有任何反应,并且在我看到的日志中
尝试在不提示位置授权的情况下启动 MapKit 位置更新。必须先致电
-[CLLocationManager requestWhenInUseAuthorization]或-[CLLocationManager requestAlwaysAuthorization]。
我不明白我做错了什么。我对编程很陌生,我看到的所有信息对我来说都没有多大意义。
我在我的应用中使用故事板,具体代码如下:
mapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface mapViewController : UIViewController {
MKMapView *mapview;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapview;
-(IBAction)setMap:(id)sender;
-(IBAction)getCurrentLocation:(id)sender;
@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
@end
mapViewController.m
#import "mapViewController.h"
@interface mapViewController ()
@end
@implementation mapViewController {
CLLocationManager *locationManager;
}
@synthesize mapview;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.requestWhenInUseAuthorization;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapview.mapType = MKMapTypeStandard;
break;
case 1:
mapview.mapType = MKMapTypeSatellite;
break;
case 2:
mapview.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
-(IBAction)getCurrentLocation:(id)sender {
mapview.showsUserLocation = YES;
}
@end
我已经为此苦苦挣扎了很长时间,以至于我的头发都被扯掉了。谁能指出我哪里出错了?
【问题讨论】: