Demo線上庫: https://github.com/Xcodemonkey/RunPathDemo.git
效果圖:
------ 一 :配置 ------
此處官方文檔說明很詳細(xì),這里就簡(jiǎn)單說一下就行了.
申請(qǐng)AK -->(如需使用雷達(dá)周邊功能,發(fā)現(xiàn)太長(zhǎng)了,寫在下一篇,先吃飯先.)注冊(cè)雷達(dá)功能 --> 下載SDK -->導(dǎo)入SDK及相關(guān)依賴庫及基礎(chǔ)資源(mapapi.bundle)(可使用pod導(dǎo)入免去到依賴庫的麻煩,但是SDK相對(duì)較大,耗時(shí)長(zhǎng),且我們可能只需其中的一些功能.) --> 配置info.plist文件(定位授權(quán)/后臺(tái)等),也可以代碼申請(qǐng)授權(quán)
至此完畢,另外再次鄭重提出:
一: Xcode7 與 Xcode8 配置授權(quán)的key是有差別的,嘗試過在Xcode7上用Xcode8的key來配置發(fā)現(xiàn)是無效的
二:百度SDK使用了C++混編,可按官網(wǎng)配置編譯方式,也可改變?nèi)我庖粋€(gè)文件后綴為.mm.
Xcode8的key:
<key>Privacy - Location Always Usage Description</key>
<string>只有開啟定位功能才能正常使用百度導(dǎo)航</string>
<key>Privacy - Location When In Use Usage Description</key>
<string>只有開啟定位功能才能正常使用百度導(dǎo)航</string>
Xcode7的key:
<key>NSLocationAlwaysUsageDescription</key>
<true/>
<key>NSLocationWhenInUseUsageDescription</key>
<true/>
------ 二 :申請(qǐng)百度地圖授權(quán) ------
在app啟動(dòng)完成的回調(diào)中創(chuàng)建地圖管理者(BMKMapManager)并申請(qǐng)地圖授權(quán)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 要使用百度地圖扁耐,請(qǐng)先啟動(dòng)BaiduMapManager
self.mapManager = [[BMKMapManager alloc]init];
// 如果要關(guān)注網(wǎng)絡(luò)及授權(quán)驗(yàn)證事件,請(qǐng)?jiān)O(shè)定 generalDelegate參數(shù)
BOOL ret = [_mapManager start:BMKKey generalDelegate:nil];
if (!ret) {
NSLog(@"manager start failed!");
}
self.window = [[UIWindow alloc] initWithFrame:SCREEN_BOUNDS];
BaseTabBarController *rootVc = [[BaseTabBarController alloc] init];
self.window.rootViewController = rootVc;
[self.window makeKeyAndVisible];
// //由于IOS8中定位的授權(quán)機(jī)制改變 需要進(jìn)行手動(dòng)授權(quán)
// CLLocationManager *locationManager = [[CLLocationManager alloc] init];
// //獲取授權(quán)認(rèn)證
// [locationManager requestAlwaysAuthorization];
// [locationManager requestWhenInUseAuthorization];
return YES;
}
------ 三 :設(shè)置mapView及l(fā)ocationService屬性 ------
在map view 和 locationService的懶加載中設(shè)置需要的相關(guān)屬性,最重要的是delegate,其他不多說.想看的自己下我的Demo吧,里面注釋非常詳細(xì).線上庫地址在文章開頭.
------ 四 :設(shè)置必要時(shí)的釋放------
- (void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此處記得不用的時(shí)候需要置nil浩村,否則影響內(nèi)存的釋放
}
- (void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用時(shí)做葵,置nil
}
- (void)dealloc {
_mapView.delegate = nil; // 不用時(shí),置nil
}
------ 五 :遵守協(xié)議 并 按需要實(shí)現(xiàn)相應(yīng)代理方法 ------
SDK中注釋很全面,自己根據(jù)需求自己選擇,該Demo中選擇了以下幾個(gè):
#pragma mark - **** BMKMapViewDelegate ****
/** 地圖加載完成 */
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
// 開啟定位服務(wù)
[self.locService startUserLocationService];
}
/** 大頭針樣式,調(diào)用- (void)addAnnotation:(id <BMKAnnotation>)annotation;后回調(diào) */
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation;
/** 路徑樣式,調(diào)用- (void)addOverlay:(id <BMKOverlay>)overlay后回調(diào) */
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay;
#pragma mark - **** BMKLocationServiceDelegate ****
/** 用戶位置更新時(shí)回調(diào),在此方法中記錄行走路徑上所經(jīng)過的所有點(diǎn),并根據(jù)所記錄的數(shù)組生成軌跡并加到mapview中*/
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
// 當(dāng)前位置信息
[_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
[_mapView updateLocationData:userLocation];
self.currentLocation = userLocation.location;
[self.allLocations addObject:_currentLocation];
// 生成的行走軌跡
CLLocationCoordinate2D *coordinates = malloc(sizeof(CLLocationCoordinate2D) *self.allLocations.count);
for (int i = 0; i < self.allLocations.count; i ++) {
coordinates[i] = [self.allLocations[i] coordinate];
}
// 過濾,添加前移除先前的
if (self.routeLine) {
[self.mapView removeOverlay:self.routeLine];
}
self.routeLine = [BMKPolyline polylineWithCoordinates:coordinates count:self.allLocations.count];
if (nil != self.routeLine) {
[self.mapView addOverlay:self.routeLine];
}
}
到此,繪制路徑完成,Demo中有每10秒超過100米位移就會(huì)打一個(gè)大頭針,大頭針點(diǎn)擊顯示的是該點(diǎn)的路經(jīng)時(shí)間;有興趣的自己去下載看看,順手點(diǎn)個(gè)星也不是不可以的??.
code: @XiYue on https://github.com