CLLocationManager位置服务对象和地图的简单结合使用

来源:互联网 发布:制作淘宝店铺标志 编辑:程序博客网 时间:2024/06/07 00:03

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    // 1.创建地图视图对象

    _mapView = [[MKMapViewalloc] initWithFrame:self.view.bounds];

    // 设置地图模式

    _mapView.mapType =MKMapTypeStandard;

    [self.viewaddSubview:_mapView];

    

    // 2.创建位置服务对象

    _locationManager = [[CLLocationManageralloc] init];

    // 设置代理的对象

    _locationManager.delegate =self;

    // 设置精准度

    [_locationManagersetDesiredAccuracy:kCLLocationAccuracyBest];

    // 如需定位服务

    /* iOS8.0如需使用位置服务

     requestAlwaysAuthorization -->询问是否允许访问你的位置(永久授权)

     NSLocationAlwaysUsageDescription -->requestWhenInUseAuthorization(使用中授权)

     注意:此处需要在plist文件中配置key

     1.永久授权设置 NSLocationAlwaysUsageDescription (配置Value可以设置字符串,会显示在弹出视图中)

     2.使用中授权设置 NSLocationWhenInUseUsageDescription (配置Value可以设置字符串,会显示在弹出视图中)

     */

   if ([_locationManagerrespondsToSelector:@selector(requestWhenInUseAuthorization)]) {

//        [_locationManager requestAlwaysAuthorization];

        [_locationManagerrequestWhenInUseAuthorization];

    }

    // 开始定位

    [_locationManagerstartUpdatingLocation];

    

}

/*

 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.停止定位

    [_locationManagerstopUpdatingLocation];

    

    // 2.设置地图现实自身位置

    _mapView.showsUserLocation =YES;

    

    // 3.获取当前手机位置

   CLLocation *location = [locations lastObject];

//    

//     4.设置地图显示区域

//     地图缩放比例

    MKCoordinateSpan span =MKCoordinateSpanMake(.1,.1);

    // 地图显示区域

   MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, span);

    [_mapViewsetRegion:region];

    

}



0 0
原创粉丝点击