最近項目須要作到這個功能,寫篇博客,只是自我總結,沒有時間寫demo,先上效果圖:bash
先說思路,咱們的項目是一秒走一個經緯度及畫一條線段,可前進後退,先用定時器重複執行一個方法,方法就處理小點前進的動畫,繪製路線我是每次清空,從新繪製的,設置一個全局變量,記錄下當下小點移動到哪一個位置,此次好進行前進後退的處理,思路其實很簡單的app
#pragma mark---------劃線代理回調----------
- (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKPolyline class]]) {
BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
polylineView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:1];
polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
polylineView.lineWidth = 2.0f;
return polylineView;
}
return nil;
}
複製代碼
BMKPointAnnotation * antion = [[BMKPointAnnotation alloc]init];
antion.title = @"";
antion.coordinate = _coors[_index-1];
[_mapView addAnnotation:antion];
//重複再添加一次起點和終點
KJBHomeHistoricalTrackAnnotation * Annotation = [[KJBHomeHistoricalTrackAnnotation alloc]init];
Annotation.type =0;
Annotation.coordinate = _coors[0];
[_mapView addAnnotation:Annotation];
Annotation = [[KJBHomeHistoricalTrackAnnotation alloc] init];
Annotation.type = 1;
Annotation.coordinate = _coors[Max];
[_mapView addAnnotation:Annotation];
//軌跡繪製
BMKPolyline * polyline = [BMKPolyline polylineWithCoordinates:_coors count:_index];
[_mapView addOverlay:polyline];
複製代碼
// 根據polyline設置地圖範圍
- (void)mapViewFitPolyLine:(BMKPolyline *) polyLine {
CGFloat ltX, ltY, rbX, rbY;
if (polyLine.pointCount < 1) {
return;
}
BMKMapPoint pt = polyLine.points[0];
ltX = pt.x; ltY = pt.y;
rbX = pt.x; rbY = pt.y;
for (int i = 1; i < polyLine.pointCount; i++) {
BMKMapPoint pt = polyLine.points[i];
if (pt.x < ltX) {
ltX = pt.x;
}
if (pt.x > rbX) {
rbX = pt.x;
}
if (pt.y > ltY) {
ltY = pt.y;
}
if (pt.y < rbY) {
rbY = pt.y;
}
}
BMKMapRect rect;
rect.origin = BMKMapPointMake(ltX , ltY);
rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY);
[_mapView setVisibleMapRect:rect];
}複製代碼
到這裏,代碼就結束了,在viewWillDisappear中別忘了設置_mapView.delegate爲nil,動畫
定時器也要及時釋放調ui