IOS學習之——地圖2 跟蹤使用者位置變化

來源:互聯網
上載者:User

標籤:定位   跟蹤   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 跟蹤使用者位置變化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.