CoreLocation MKMapView

高德開發者平臺 有開發指南html

iOS9配置網絡:git

<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>api

請看這裏  原文章:http://www.oschina.net/question/262659_149771?fromerr=Y0rzKueR網絡

1. GPS定位:<CoreAudioKit/CoreAudioKit.h>

1. 基本屬性:

1>框架

CLLocationManager:定位管理器   協議:<CLLocationManagerdelegate> 設置代理 實現方法編碼

CLLocation:位置的具體信息(經緯度 等等)spa

CLHeading:設備移動方向.net

CLRegion:一個區域(經常使用子類:CLCircularRegion:圓形 CLBeaconRegion:藍牙代理

[CLLocationManager locationServicesEnabled] 定位服務是否可用code

distanceFilter:自動過濾距離 移動某個距離以後從新調用代理方法 更新位置

desiredAccuracy:定位的精度

self.manager.desiredAccuracy = kCLLocationAccuracyBest; // 最佳精度
self.manager.pausesLocationUpdatesAutomatically = YES; // 不須要的時候能夠自動暫停

 

- (void)viewDidLoad {
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    
// 容許定位
    [self.locationManager requestAlwaysAuthorization];
    
// 自動過濾距離 移動100米以後從新調用代理方法 更新位置
    self.locationManager.distanceFilter = 100.0;  // 米爲單位
    
// iOS7的系統下 寫完start就能夠開始定位了
    [self.locationManager startUpdatingLocation];
    
// 初始化地理編碼器:
    self.geocoder = [CLGeocoder new];
}

2> CLGeocoder 地理編碼器:

建立:

self.geocoder = [CLGeocoder new];

編碼:提供某個字符串 來定位位置:- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;

[self.geocoder geocodeAddressString:self.inputLocation.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        // 取出一個位置信息
        CLPlacemark *placeMark = placemarks.lastObject;
        // 輸出信息
        NSLog(@"%lf   %lf", placeMark.location.coordinate.latitude, placeMark.location.coordinate.longitude);
    }];

反編碼:根據位置顯示該地方的名字等等

[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *place = placemarks.lastObject;
        self.inputLocation.text = place.name;
        NSLog(@" %@",place.name);
    }];

2. 獲取位置信息:

iOS7的系統下 寫完start就能夠開始定位了:

[self.locationManager startUpdatingLocation];

 

可是在iOS以後就須要設置是否容許定位:設置完成這個以後才能夠定位

requestAlwaysAuthorization:一直容許定位

requestWhenInUseAuthorization:用戶容許

在添加以前須要在info.plist 文件中添加字段:NSLocationAlwaysUsageDescription  (後面的字符串知識提示的時候會顯示 並無什麼用)

[self.locationManager requestAlwaysAuthorization];

2. 地圖:<MapKit/MapKit.h>

1> iOS原生地圖:

前面帶MK的是系統自帶的地圖:

MKUserLocation:地圖上的大頭針 有title subtitle等屬性

MKMapView:用來顯示地圖 與視圖同樣 初始化須要肯定frame 定位的時候須要用到coreLocation框架

showsUserLocation 設置爲YES 容許跟蹤定位 (MKMapView的屬性)

可自定義MKAnnotation

//     建立比例係數 顯示在某個點上
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.1, 0.1)) ;
//     比例係數越小 放大效果越大
    self.mapView.region = region; // 系統自帶

2> 高德:

多以MA開頭的:

[_mapView setZoomLevel:16.0 animated:YES];  設置縮放的比例

// 1. 驗證key
    [MAMapServices sharedServices].apiKey = @「申請的key」;
    // 2. 初始化
    mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.bounds))];
    mapView.delegate = self;
    //    mapView.language = MAMapLanguageEn; // 設置地圖顯示語言
    mapView.mapType = MAMapTypeStandard; // 地圖類型
    /*
     MAMapTypeSatellite:衛星地圖
     MAMapTypeStandard:標準地圖
     */
//    mapView.showTraffic = YES; // 顯示實時交通路況
    [self.view addSubview:mapView];
    mapView.showsUserLocation = YES;

mapView的定位模式: userTrackingMode

MAUserTrackingModeNone:不跟隨用戶位置,僅在地圖上顯示。

MAUserTrackingModeFollow:跟隨用戶位置移動,並將定位點設置成地圖中心點

MAUserTrackingModeFollowWithHeading:跟隨用戶的位置和角度移動

系統的地圖和 高德地圖 的區別:http://www.mamicode.com/info-detail-573898.html

相關文章
相關標籤/搜索