Core Location_定位功能

//git

//  ViewController.mspa

//  Core Location_定位功能.net

//3d

//  Created by dc008 on 15/12/23.代理

//  Copyright © 2015 崔晓宇. All rights reserved.orm

//ip



//1.只要须要定位功能。。。在plist中添加。。get

//NSLocationWhenInUseUsageDescription>YES(当使用时)it

//NSLocationAlwaysUsageDescription>YES(一直)io

#import "ViewController.h"

//2.引入头文件

#import <CoreLocation/CoreLocation.h>

#import <MapKit/MapKit.h>//地图库                        //引入地图代理

#import "CXYAnnotation.h"

@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>

//3.建立定位管理

{

    CLLocationManager *_locationManager;

    //定义地图视图

    MKMapView *_mapView;

    

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    _mapView = [[MKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    

    //设置用户位置追踪

    _mapView.userTrackingMode = MKUserTrackingModeFollow;

    //设置地图类型

    _mapView.mapType = MKMapTypeHybrid;

    _mapView.delegate = self;

    

    

    [self.view addSubview:_mapView];

    //初始化管理器

    _locationManager = [[CLLocationManager alloc]init];

    //判判定位服务是否开启

    if ( ![CLLocationManager locationServicesEnabled]) {

        NSLog(@"定位服务当前可能还没有打开,请设置打开!");

        return;

    }

    else{

        NSLog(@"定位服务已经打开");

    }

    //若是没有受权,则请求用户受权

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {

        //若是没,请求受权

        [_locationManager requestWhenInUseAuthorization];

    }

    //若是受权状态为whenInUse

    if([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusAuthorizedWhenInUse){

        //设置管理器

        //设置管理器代理--CLLocationManagerDelegate

        _locationManager.delegate = self;

        //设置定位精度

        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        //设置频率

        _locationManager.distanceFilter = 100;

        //启动定位

        [_locationManager startUpdatingLocation];

    }

    

    [self addAnnotation];

}


#pragma mark 添加大头针

- (void)addAnnotation{

    CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(30.7266819435, 120.7208981251);

    CXYAnnotation *annonation1 = [[CXYAnnotation alloc]init];

    annonation1.title = @"智慧创业创新园";

    annonation1.subtitle = @"东臣信息科技";

    annonation1.coordinate = location1;

    [_mapView addAnnotation:annonation1];

    

}


- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

    NSLog(@"1");

    //将地图中心位置移动到某点

//    _mapView setCenterCoordinate:<#(CLLocationCoordinate2D)#>

    //地图显示区域

    MKCoordinateRegion theRegion;

    theRegion.center = userLocation.coordinate;

    theRegion.span.latitudeDelta = 0.1;

    theRegion.span.longitudeDelta = 0.1;

    [_mapView setRegion:theRegion];

}


#pragma mark CoreLocation代理

#pragma mark 跟踪定位的代理方法,每次位置发生变化就会执行

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {

    CLLocation *location = [locations firstObject];//取出位置信息

    CLLocationCoordinate2D coordinate =location.coordinate;//获取位置坐标

    NSLog(@"经度为:%f",coordinate.longitude);

    NSLog(@"纬度为:%f",coordinate.latitude);

    NSLog(@"海拔:%f, 航向:%f, 行走速度:%f",location.altitude, location.course, location.speed);

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

相关文章
相关标签/搜索