標籤:定位 跟蹤 mapkit ios
歡迎轉載,轉載請註明出處
本文地址:http://blog.csdn.net/zhenggaoxing/article/details/42707685
綜述
mapkit提供了跟蹤使用者位置和方向變化的API,所以我們這裡不用自定編輯定位資訊,交給系統來搞。
添加framework
實現授權擷取位置資訊
在iOS8中,沒有使用者授權程式是無法擷取定位資訊的,所以我們需要在info.plist上添加兩個鍵值:
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
同時給self(viewcontroller)添加CLLocationmanager 類型的屬性
#import <UIKit/UIKit.h>#import <MapKit/MapKit.h>#import <CoreLocation/CLLocationManagerDelegate.h>@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet MKMapView *mapView;@property (strong,nonatomic) CLLocationManager *locationManage;
然後實現請求授權方法:
self.locationManage=[CLLocationManager new]; [self.locationManage requestAlwaysAuthorization];
請求授權的方法有兩個:
requestAlwaysAuthorization 總是授權
requestWhenInUseAuthorization 試用的時候授權
實現之後的效果:(使用者允許你擷取位置資訊)
實現跟蹤
設定跟蹤的方式
if([CLLocationManager locationServicesEnabled]) { mapView.mapType=MKMapTypeStandard; mapView.delegate=self; mapView.showsUserLocation=YES; [mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];// 設定跟蹤方式 }跟蹤的方式有三種可選的
MKUserTrackingModeNone 不跟蹤
MKUserTrackingModeFollow 跟蹤
MKUserTrackingModeFollowWithHeading 跟蹤擷取位置資訊和方向資訊
即時更新跟蹤的位置資訊
MKUserTrackingModeNone = 0, // the user's location is not followedMKUserTrackingModeFollow, // the map follows the user's locationMKUserTrackingModeFollowWithHeading,
看看效果:是不是很酷?但是:範圍是不是大了點?
設定顯示範圍(region)添加代碼如下:
MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(mapView.centerCoordinate, 1000, 1000); [mapView setRegion:viewRegion animated:YES];
第一行代碼設定三個參數意義:原點,南北,東西詳見: IOS學習之——地圖1 顯示地圖 +現實地圖
看看最終效果:
OS學習之——定位服務4 設定測試位置調試測試位置
原始碼:
https://git.oschina.net/zhengaoxing/No15.2.2follow
IOS學習之——地圖2 跟蹤使用者位置變化