- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1.创建地图视图对象
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
// 设置地图模式
_mapView.mapType = MKMapTypeStandard;
[self.view addSubview:_mapView];
// 2.创建位置服务对象
_locationManager = [[CLLocationManager alloc] init];
// 设置代理的对象
_locationManager.delegate = self;
// 设置精准度
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// 如需定位服务
/* iOS8.0如需使用位置服务
requestAlwaysAuthorization -->询问是否允许访问你的位置(永久授权)
NSLocationAlwaysUsageDescription -->requestWhenInUseAuthorization(使用中授权)
注意:此处需要在plist文件中配置key
1.永久授权设置 NSLocationAlwaysUsageDescription (配置Value可以设置字符串,会显示在弹出视图中)
2.使用中授权设置 NSLocationWhenInUseUsageDescription (配置Value可以设置字符串,会显示在弹出视图中)
*/
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
// [_locationManager requestAlwaysAuthorization];
[_locationManager requestWhenInUseAuthorization];
}
// 开始定位
[_locationManager startUpdatingLocation];
}
/*
CLLocation用来表示某个位置的地理信息,比如经纬度、海拔等等
(1)经纬度
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
(2)海拔
@property(readonly, nonatomic) CLLocationDistance altitude;
(3)路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)
@property(readonly, nonatomic) CLLocationDirection course;
(4)行走速度(单位是m/s)
@property(readonly, nonatomic) CLLocationSpeed speed;
(5)计算2个位置之间的距离
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location方法
*/
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// 1.停止定位
[_locationManager stopUpdatingLocation];
// 2.设置地图现实自身位置
_mapView.showsUserLocation = YES;
// 3.获取当前手机位置
CLLocation *location = [locations lastObject];
//
// 4.设置地图显示区域
// 地图缩放比例
MKCoordinateSpan span = MKCoordinateSpanMake(.1, .1);
// 地图显示区域
MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, span);
[_mapView setRegion:region];
}