//
// ViewController.m
// Core Location_定位功能
//
// Created by dc008 on 15/12/23.
// Copyright © 2015年 崔晓宇. All rights reserved.
//
//1.只要需要定位功能。。。在plist中添加。。
//NSLocationWhenInUseUsageDescription>YES(当使用时)
//NSLocationAlwaysUsageDescription>YES(一直)
#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