//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;
}
@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