Permalink
Browse files

Initial commit

Added readme and the first stub of the classes
  • Loading branch information...
0 parents commit a268a17b70ce5fd2f974bfe8054d672584cf5c80 @elbryan committed Jul 19, 2010
Showing with 210 additions and 0 deletions.
  1. +1 −0 .gitattributes
  2. +13 −0 .gitignore
  3. +20 −0 FFMapRoute.h
  4. +18 −0 FFMapRoute.m
  5. +33 −0 FFMapRoutes.h
  6. +110 −0 FFMapRoutes.m
  7. +15 −0 README.markdown
@@ -0,0 +1 @@
+*.pbxproj -crlf -diff -merge
@@ -0,0 +1,13 @@
+# xcode noise
+*.mode1v3
+*.pbxuser
+*.perspective
+*.perspectivev3
+*.pyc
+*~.nib/
+build/*
+
+# osx noise
+.DS_Store
+profile
+*.mode2v3
@@ -0,0 +1,20 @@
+//
+// FFMapRoute.h
+// iScout
+//
+// Created by Fabiano Francesconi on 19/07/10.
+// Copyright 2010 Fabiano Francesconi. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <MapKit/MapKit.h>
+
+@interface FFMapRoute : NSObject {
+ NSUInteger level;
+ MKPolyline *line;
+}
+
+@property (nonatomic, retain) MKPolyline *line;
+@property (nonatomic, assign) NSUInteger level;
+
+@end
@@ -0,0 +1,18 @@
+//
+// FFMapRoute.m
+// iScout
+//
+// Created by Fabiano Francesconi on 19/07/10.
+// Copyright 2010 Fabiano Francesconi. All rights reserved.
+//
+
+#import "FFMapRoute.h"
+
+@implementation FFMapRoute
+
+- (void) dealloc {
+ [line dealloc];
+ [super dealloc];
+}
+
+@end
@@ -0,0 +1,33 @@
+//
+// FFMapRoute.h
+// iScout
+//
+// Created by Fabiano Francesconi on 10/07/10.
+// Copyright 2010 Fabiano Francesconi. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <CoreLocation/CoreLocation.h>
+#import <MapKit/MapKit.h>
+#import "FFMapRoute.h"
+
+/* Each kAGGREGATION_FACTOR points, aggregate those in a single polyline */
+#define kAGGREGATION_FACTOR 10
+
+@interface FFMapRoutes: NSObject {
+ NSMutableArray *points;
+ NSMutableArray *lines;
+
+ MKMapView *mapView;
+
+ CLLocationCoordinate2D lastCoordinate;
+
+ /* Array of routes */
+ FFMapRoute **routes;
+}
+
+@property (nonatomic, assign) MKMapView *mapView;
+
+- (void) addCoordinate:(CLLocationCoordinate2D)coordinate;
+
+@end
@@ -0,0 +1,110 @@
+//
+// FFMapRoute.m
+// iScout
+//
+// Created by Fabiano Francesconi on 10/07/10.
+// Copyright 2010 Fabiano Francesconi. All rights reserved.
+//
+
+#import "FFMapRoutes.h"
+
+@interface FFMapRoutes ()
+- (void) addOverlayToMapViewWithCoordinate:(CLLocationCoordinate2D) coordinate;
+- (void) manageOverlaysInMapView;
+
+@property (nonatomic, retain) NSMutableArray *points;
+@property (nonatomic, retain) NSMutableArray *lines;
+@property (nonatomic, assign) CLLocationCoordinate2D lastCoordinate;
+@end
+
+@implementation FFMapRoutes
+
+- (id) init {
+ if (self = [super init]) {
+ points = [[NSMutableArray alloc] init];
+ lines = [[NSMutableArray alloc] init];
+ *routes = malloc(class_getInstanceSize([FFMapRoute class]) * kAGGREGATION_FACTOR);
+ }
+
+ return self;
+}
+
+- (void) dealloc {
+ [points release];
+ [lines release];
+ [super dealloc];
+}
+
+#pragma mark -
+
+- (void) addCoordinate:(CLLocationCoordinate2D)coordinate {
+ /* Wrap the coordinate inside a NSData object */
+ NSData *value = [NSData dataWithBytes:&coordinate length:sizeof(CLLocationCoordinate2D)];
+ [points addObject:value];
+
+ [self addOverlayToMapViewWithCoordinate:coordinate];
+}
+
+- (void) addOverlayToMapViewWithCoordinate:(CLLocationCoordinate2D) coordinate {
+ /* If we don't have a last coordinate, then wait for a second point */
+ if ((lastCoordinate.latitude == 0) && (lastCoordinate.longitude == 0)) {
+ lastCoordinate = coordinate;
+ return;
+ }
+
+ /* If we have kAGGREGATION_FACTOR segments, aggregate those */
+ if ([lines count] == kAGGREGATION_FACTOR) {
+ [self manageOverlaysInMapView];
+ } else {
+ /* Add the new segment */
+ MKMapPoint *mapPoints = malloc(sizeof(MKMapPoint) * 2);
+ mapPoints[0] = MKMapPointForCoordinate(lastCoordinate);
+ mapPoints[1] = MKMapPointForCoordinate(coordinate);
+
+ /* Generate the polyline */
+ MKPolyline *segmentLine = [MKPolyline polylineWithPoints:mapPoints count:2];
+
+ /* Add the segment to the mapview overlays */
+ [mapView addOverlay:segmentLine];
+ [lines addObject:segmentLine];
+
+ free(mapPoints);
+ }
+
+ lastCoordinate = coordinate;
+}
+
+- (void) manageOverlaysInMapView {
+ /* Aggregate all the points into a single polyline */
+ MKMapPoint *mapPoints = malloc(sizeof(MKMapPoint) * [points count]);
+ int idx = 0;
+
+ for (NSData *value in points) {
+ CLLocationCoordinate2D *coordinate = (CLLocationCoordinate2D *) [value bytes];
+ MKMapPoint point = MKMapPointForCoordinate(*coordinate);
+ mapPoints[idx] = point;
+ idx++;
+ }
+
+ /* Generate the new line */
+ MKPolyline *segmentLine = [MKPolyline polylineWithPoints:mapPoints count:[points count]];
+
+ /* Discard the old polyline */
+ NSMutableArray *discardedLines = [NSMutableArray array];
+
+ for (MKPolyline *line in lines) {
+ [discardedLines addObject:line];
+ [mapView removeOverlay:line];
+
+ printf("Removed an overlay\n");
+ }
+
+ [lines removeObjectsInArray:discardedLines];
+
+ /* Add the new polyline */
+ [mapView addOverlay:segmentLine];
+ [lines addObject:segmentLine];
+
+ free(mapPoints);
+}
+@end
@@ -0,0 +1,15 @@
+# MapKitDynRoutes
+
+iOS4 library to manage dynamic routes on MKMapView.
+This library tries to handle the lack of methods to manage growing routes without
+repainting all of those anytime.
+
+## Thanksgiving
+I want to say thanks to Walter 'DaK_TaLeS' for spending few nights talking about how to develop the algorithm used by this library and for supporting (and injuring) me all the time.
+
+## License
+
+This library and the associated demo are licensed under MIT license.
+However, if you are using this library and you want to share with me the fact that it suits your needs, please drop me a line. I'll be glad to hear that.
+
+Fabiano aka 'elbryan'

0 comments on commit a268a17

Please sign in to comment.