-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFFMapRoute.m
76 lines (58 loc) · 1.67 KB
/
FFMapRoute.m
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
//
// FFMapRoute.m
//
// Created by Fabiano Francesconi on 19/07/10.
// Copyright 2010 Fabiano Francesconi. All rights reserved.
//
#import "FFMapRoute.h"
@interface FFMapRoute ()
- (void) generatePolyline;
@end
@implementation FFMapRoute
- (id) initWithPoints:(NSArray *)newPoints {
if (self = [super init]) {
/* Retain an own copy of the segment array */
self.points = [NSMutableArray arrayWithArray:newPoints];
[self generatePolyline];
}
return self;
}
- (void) dealloc {
self.line = nil;
self.points = nil;
[super dealloc];
}
#pragma mark -
#pragma mark Public Methods
- (void) updateRouteWithPoints:(NSArray *)newPoints {
/* Drop the old data */
if (self.points) {
self.points = nil;
self.line = nil;
}
/* Generate the new one */
self.points = [NSMutableArray arrayWithArray:newPoints];
[self generatePolyline];
}
#pragma mark -
#pragma mark Private Methods
- (void) generatePolyline {
/* Prepare an array of MKMapPoint in order to prepare the polyline */
MKMapPoint *mapPoints = malloc(sizeof(MKMapPoint) * [points count]);
int idx=0;
/* Fill mapPoints array with the coordinate converted in MKMapPoint */
for (NSData *value in points) {
CLLocationCoordinate2D *coordinate = (CLLocationCoordinate2D *) [value bytes];
MKMapPoint point = MKMapPointForCoordinate(*coordinate);
mapPoints[idx] = point;
idx++;
}
/* Generate the polyline */
MKPolyline *segmentLine = [MKPolyline polylineWithPoints:mapPoints count:[points count]];
/* Free the array */
free(mapPoints);
/* Retain the line. We need it in order to remove this (that's going to be added to the mapView
in form of overlay) when aggregation occurs */
self.line = segmentLine;
}
@end