近日狀況
好久沒更新了跟压,因為換了份工作擦囊,當(dāng)前公司項目在都在忙著加班趕項目馆类,現(xiàn)在項目快結(jié)束了混聊,乘這時間(其實是我懶=≡Σ(((つ??ω??)つ)給更新。
瞎扯
人們生活水平高了,對自己的身體開始注意了句喜,運(yùn)動類的App開始搶占市場(我記得是14年開始的预愤,說得自己像是一個老程序員一樣) 今天我就給大家分享一篇項目中的運(yùn)動模塊對路徑繪制及重播的部分。
先給你看一下實現(xiàn)的效果:
因為在模擬器上測試咳胃,坐標(biāo)是在地圖之外植康,顯示的是灰色的
概述
- 這里是基于高德地圖的實現(xiàn)的
- 實時軌跡的繪制以及路徑重播
配置
我想來看這的對于高德地圖的配置應(yīng)該是知道的了,如果有什么疑問參考官方展懈。
這里使用的是高德地圖4.6.1版本 基礎(chǔ)SDK是1.4 記得使用高德地圖5.0.0版本的時候坐標(biāo)會飄 是的回飄销睁,如果有用最新版的可以自行測試一下,是不是還會出現(xiàn)在這樣的情況存崖。
實現(xiàn)
1.繪制
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
if (!updatingLocation) {
return;
}
if (self.isRecording)
{
//傳感器活動狀態(tài)
// BOOL deviceActivity = ([StepSampleManage shareManage].deviceActivityState != SADeviceActivityStateNotMoving && [StepSampleManage shareManage].deviceActivityState != SADeviceActivityStateUnknown);
// if (SIMULATOR_TEST) {
// deviceActivity = YES;
// }
BOOL deviceActivity = YES;
if (userLocation.location.horizontalAccuracy < 40 && userLocation.location.horizontalAccuracy > 0 && deviceActivity)
{
if (self.currentRecord.totalDistance >= 100 && !self.existStartPointed) {
MAPointAnnotation *annotation = [[MAPointAnnotation alloc]init];
annotation.coordinate = self.currentRecord.coordinates[0];
[mapView addAnnotation:annotation];
self.existStartPointed = YES;
}
[self.locationsArray addObject:userLocation.location];
NSLog(@"date: %@,now :%@",userLocation.location.timestamp,[NSDate date]);
[self.tipView showTip:[NSString stringWithFormat:@"has got %ld locations",self.locationsArray.count]];
[self.currentRecord addLocation:userLocation.location];
[self.mutablePolyline appendPoint:MAMapPointForCoordinate(userLocation.location.coordinate)];
[self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
[self.mutableView referenceDidChange];
}
}
#ifdef DEBUG
[self.statusView showStatusWith:userLocation.location];
#endif
}
通過代理獲取的坐標(biāo)的傳入Record模型中冻记,這里的模型是一個Demo的模型,你們可以根據(jù)自己的實際情況定義
mutablePolyline和mutableView相當(dāng)于畫筆和畫板来惧,沒錯這樣去理解(大概就是這個意思)獲取到的點在地圖變成線
2.播放
播放這里使用mutablePolyline和MAMutablePolylineRenderer *mutableView 是一個能提供有多種顏色的畫板去理解冗栗,讀取之前保存的記錄 (我之前實際項目是保存在數(shù)據(jù)庫中的) 這里主要是播放速度和播放點的
控制
- (void)animateToNextCoordinate
{
if (self.myLocation == nil)
{
return;
}
CLLocationCoordinate2D *coordinates = [self.record coordinates];
if (self.currentLocationIndex == [self.record numOfLocations] )
{
self.currentLocationIndex = 0;
[self actionPlayAndStop];
return;
}
CLLocationCoordinate2D nextCoord = coordinates[self.currentLocationIndex];
CLLocationCoordinate2D preCoord = self.currentLocationIndex == 0 ? nextCoord : self.myLocation.coordinate;
self.heading = [self coordinateHeadingFrom:preCoord To:nextCoord];
CLLocationDistance distance = MAMetersBetweenMapPoints(MAMapPointForCoordinate(nextCoord), MAMapPointForCoordinate(preCoord));
NSTimeInterval duration = distance / (self.averageSpeed * 1000 * self.multiple);
CLLocationCoordinate2D coords[2];
coords[0] = preCoord;
coords[1] = nextCoord;
@weakify(self)
[self.myLocation addMoveAnimationWithKeyCoordinates:coords count:2 withDuration:duration + 0.01 withName:kUserLoctionAnimatedKey completeCallback:^(BOOL isFinished) {
@strongify(self)
self.currentLocationIndex ++;
self.playSlier.value = (float)self.currentLocationIndex/[self.record numOfLocations];
if (isFinished) {
[self animateToNextCoordinate];
}
}];
self.myLocation.movingDirection = self.heading;
}
獲取保存的當(dāng)前坐標(biāo)點和上一個點的動畫時間
動畫時間 = 距離 / (播放速度*播放倍數(shù))
for(MAAnnotationMoveAnimation *animation in [self.myLocation allMoveAnimations]) {
if ([animation.name isEqualToString:kUserLoctionAnimatedKey]) {
[animation cancel];
}
}
使用上述的方法去取消動畫,如果對方向有要求的記錄一下停止之前的方向然后再試下
self.myLocation.movingDirection = self.heading;
以上就是整個繪制和播放了
- 如果后臺繪制需要后臺定位權(quán)限
- 如果暫停的時候供搀,我們要處理這過程可能發(fā)生的情況
- 為了節(jié)省空間沒有上傳地圖隅居,demo克隆下來后重新pod update
- 如果有什么問題,可以提出來大家討論一下類似我的位置飄的問題葛虐,我這里是通過陀螺儀的來判斷的
源碼地址:路徑繪制及播放
參考
https://github.com/amapapi/iOS_3D_RecordPath
結(jié)束語
- 還有就是...就是...那個...那個...Demo是簡化版的胎源,搬過來有點花時間和上面的UI有不一樣的地方 ??
- 下一次更新陀螺儀獲取步數(shù)
- 如果對你有所幫助,請我給我一顆star作為鼓勵