Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Code improvement

* Removed useless reallocation when placeholder is hit
* Refactored addPoints method (now called updateRouteWithPoints)
  • Loading branch information...
commit 4e1319b7ed000eebddf72448ede50679a9c0ccfa 1 parent 12b482a
@elbryan authored
View
11 Demo/Demo.xcodeproj/project.pbxproj
@@ -252,13 +252,14 @@
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
- IPHONEOS_DEPLOYMENT_TARGET = 3.1.3;
+ IPHONEOS_DEPLOYMENT_TARGET = 4.0;
OTHER_CFLAGS = (
"-Xclang",
"-fobjc-nonfragile-abi2",
);
PREBINDING = NO;
SDKROOT = iphoneos4.0;
+ TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
@@ -268,11 +269,17 @@
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
GCC_C_LANGUAGE_STANDARD = c99;
+ GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
- OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
+ IPHONEOS_DEPLOYMENT_TARGET = 4.0;
+ OTHER_CFLAGS = (
+ "-Xclang",
+ "-fobjc-nonfragile-abi2",
+ );
PREBINDING = NO;
SDKROOT = iphoneos4.0;
+ TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
View
8 FFMapRoute.h
@@ -18,10 +18,12 @@
@property (nonatomic, retain) NSMutableArray *points;
@property (nonatomic, assign) NSUInteger level;
-/* Init the object with a segment */
-- (id) initWithSegment:(NSArray *)segment;
+/* Init the object with a list of points */
+- (id) initWithPoints:(NSArray *)newPoints;
/* Add coordinate to the local array by wrapping it into a NSData object */
-- (void) addPoint:(CLLocationCoordinate2D) coordinate;
+/* This method is useful when you created a placeholder that is an empty FFMapRoute instance. */
+/* With this method you can drop the existing points and regenerate the new polyline */
+- (void) updateRouteWithPoints:(NSArray *)newPoints;
@end
View
67 FFMapRoute.m
@@ -8,36 +8,17 @@
#import "FFMapRoute.h"
@interface FFMapRoute ()
+- (void) generatePolyline;
@end
@implementation FFMapRoute
-- (id) initWithSegment:(NSArray *)segment {
+- (id) initWithPoints:(NSArray *)newPoints {
if (self = [super init]) {
/* Retain an own copy of the segment array */
- self.points = [NSMutableArray arrayWithArray:segment];
+ self.points = [NSMutableArray arrayWithArray:newPoints];
- /* 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;
+ [self generatePolyline];
}
return self;
@@ -53,9 +34,43 @@ - (void) dealloc {
#pragma mark -
#pragma mark Public Methods
-- (void) addPoint:(CLLocationCoordinate2D) coordinate {
- NSData *newPoint = [NSData dataWithBytes:&coordinate length:sizeof(CLLocationCoordinate2D)];
- [points addObject:newPoint];
+- (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
View
20 FFMapRoutes.m
@@ -108,22 +108,24 @@ - (void) addCoordinate:(CLLocationCoordinate2D)coordinate toArray:(NSMutableArra
/* Check if we are in a placeholder situation */
route = [array lastObject];
- /* if the polyline doesn't exist, then we have a placeholder. Remove it. */
+ /* If the polyline doesn't exist, then we have a placeholder. */
+ /* Reuse it and update its data */
if (route && (route.line == nil)) {
- [array removeLastObject];
- route = nil;
+ [route updateRouteWithPoints:segment];
}
-
- /* Then create the new route */
- route = [[FFMapRoute alloc] initWithSegment:segment];
- /* Any new route has a level value of 0 (the minor one) */
+ /* Else we have a valid route that's not going to be touched. Instance a new route for the
+ next segment */
+ else {
+ /* Then create the new route */
+ route = [[[FFMapRoute alloc] initWithPoints:segment] autorelease];
+ }
+
route.level = 0;
[array addObject:route];
/* Add the overlay to the map */
[mapView addOverlay:[route line]];
- [route release];
[segment release];
}
@@ -196,7 +198,7 @@ - (void) aggregatePoints:(NSArray *)points toArray:(NSMutableArray *)array {
}
/* Generate the new route */
- FFMapRoute *newRoute = [[FFMapRoute alloc] initWithSegment:newPoints];
+ FFMapRoute *newRoute = [[FFMapRoute alloc] initWithPoints:newPoints];
newRoute.level = ++level;
[array addObject:newRoute];
Please sign in to comment.
Something went wrong with that request. Please try again.