-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSportPathDemoViewController.mm
executable file
·162 lines (138 loc) · 5.21 KB
/
SportPathDemoViewController.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// SportPathDemoViewController.m
// IphoneMapSdkDemo
//
// Created by wzy on 16/6/15.
// Copyright © 2016年 Baidu. All rights reserved.
//
#import "SportPathDemoViewController.h"
#import "MovingAnnotationView.h"
#import "TracingPoint.h"
#import "JSONKit.h"
@interface SportPathDemoViewController ()<MovingAnnotationViewAnimateDelegate> {
BMKPointAnnotation *sportAnnotation;
MovingAnnotationView *sportAnnotationView;
NSMutableArray *_tracking;
NSMutableArray *sportNodes;//轨迹点
NSInteger sportNodeNum;//轨迹点数
NSInteger currentIndex;//当前结点
}
@end
@implementation SportPathDemoViewController
- (void)viewDidLoad {
[super viewDidLoad];
//适配ios7
if( ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 7.0)) {
self.navigationController.navigationBar.translucent = NO;
}
_mapView.zoomLevel = 17.5;
_mapView.centerCoordinate = CLLocationCoordinate2DMake(40.056898, 116.307626);
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
//初始化轨迹点
[self initSportNodes];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
}
- (void)dealloc {
if (_mapView) {
_mapView = nil;
}
}
//初始化轨迹点
- (void)initSportNodes {
_tracking = [NSMutableArray array];
//读取数据
NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sport_path" ofType:@"json"]];
if (jsonData) {
NSArray *array = [jsonData objectFromJSONData];
for (NSDictionary *dic in array) {
TracingPoint * tp = [[TracingPoint alloc] init];
tp.coordinate = CLLocationCoordinate2DMake([dic[@"lat"] doubleValue], [dic[@"lon"] doubleValue]);
tp.angle = [dic[@"angle"] doubleValue];
tp.distance = [dic[@"distance"] doubleValue];
tp.speed = [dic[@"speed"] doubleValue];
[_tracking addObject:tp];
}
}
}
//开始
- (void)start {
//show route
sportNodeNum = [_tracking count];
CLLocationCoordinate2D paths[sportNodeNum];
for (NSInteger i = 0; i < sportNodeNum; i++) {
TracingPoint * tp = _tracking[i];
paths[i] = tp.coordinate;
}
BMKPolygon *path = [BMKPolygon polygonWithCoordinates:paths count:sportNodeNum];
[_mapView addOverlay:path];
//show annotation
sportAnnotation = [[BMKPointAnnotation alloc] init];
TracingPoint * start = [_tracking firstObject];
sportAnnotation.coordinate = start.coordinate;
sportAnnotation.title = @"sport node";
[_mapView addAnnotation:sportAnnotation];
}
//runing
- (void)running {
/* Find annotation view for car annotation. */
currentIndex ++;
TracingPoint *currentNode = [_tracking objectAtIndex:currentIndex % sportNodeNum];
TracingPoint *tempNode = [_tracking objectAtIndex:(currentIndex-1) % sportNodeNum];
sportNodes = [[NSMutableArray alloc] init];
[sportNodes addObject:tempNode];
[sportNodes addObject:currentNode];
sportAnnotationView.imageView.transform = CGAffineTransformMakeRotation(tempNode.angle);
[sportAnnotationView addTrackingAnimationForPoints:sportNodes duration:tempNode.distance/tempNode.speed];
tempNode = currentNode;
}
- (void)movingAnnotationViewAnimationFinished {
NSLog(@"animate finished");
[self running];
}
#pragma mark - BMKMapViewDelegate
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
[self start];
}
//根据overlay生成对应的View
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKPolygon class]])
{
BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay];
polygonView.strokeColor = [[UIColor alloc] initWithRed:0.0 green:0.5 blue:0.0 alpha:0.6];
polygonView.lineWidth = 3.0;
return polygonView;
}
return nil;
}
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
NSLog(@"view.annotation,%f,%f",sportAnnotation.coordinate.latitude,sportAnnotation.coordinate.longitude);
}
// 根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
static NSString *reuseIndetifier = @"sportsAnnotation";
sportAnnotationView = (MovingAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
if (sportAnnotationView == nil)
{
sportAnnotationView = [[MovingAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseIndetifier];
sportAnnotationView.animateDelegate = self;
}
CGPoint centerPoint= CGPointZero;
[sportAnnotationView setCenterOffset:centerPoint];
return sportAnnotationView;
}
- (void)mapView:(BMKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
[self running];
}
@end