Skip to content

Instantly share code, notes, and snippets.

@mafis
Created August 9, 2010 08:59
Show Gist options
  • Save mafis/515168 to your computer and use it in GitHub Desktop.
Save mafis/515168 to your computer and use it in GitHub Desktop.
NSMutableArray* points = [[NSMutableArray alloc] initWithCapacity:coordinates.count];
for(int idx = 0; idx < coordinates.count; idx++)
{
Koordinate* coord = (Koordinate*)[coordinates objectAtIndex:idx];
CLLocation* currentLocation = [[[CLLocation alloc] initWithLatitude:coord.coordinate.latitude longitude:coord.coordinate.longitude] autorelease];
[points addObject:currentLocation];
}
// create our route layer view, and initialize it with the map on which it will be rendered.
routeView = [[CSMapRouteLayerView alloc] initWithRoute:points mapView:mapView];
//
// CSMapRouteLayerView.h
// mapLines
//
// Created by Craig on 4/12/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface CSMapRouteLayerView : UIView <MKMapViewDelegate>
{
MKMapView* _mapView;
NSArray* _points;
UIColor* _lineColor;
}
-(id) initWithRoute:(NSArray*)routePoints mapView:(MKMapView*)mapView;
@property (nonatomic, retain) NSArray* points;
@property (nonatomic, retain) MKMapView* mapView;
@property (nonatomic, retain) UIColor* lineColor;
@end
//
// CSMapRouteLayerView.m
// mapLines
//
// Created by Craig on 4/12/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "CSMapRouteLayerView.h"
@implementation CSMapRouteLayerView
@synthesize mapView = _mapView;
@synthesize points = _points;
@synthesize lineColor = _lineColor;
-(id) initWithRoute:(NSArray*)routePoints mapView:(MKMapView*)mapView
{
self = [super initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];
[self setBackgroundColor:[UIColor clearColor]];
[self setMapView:mapView];
[self setPoints:routePoints];
// determine the extents of the trip points that were passed in, and zoom in to that area.
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx < self.points.count; idx++)
{
CLLocation* currentLocation = [self.points objectAtIndex:idx];
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
}
MKCoordinateRegion region;
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;
self.userInteractionEnabled = NO;
[self.mapView setRegion:region];
[self.mapView setDelegate:self];
[self.mapView addSubview:self];
return self;
}
- (void)drawRect:(CGRect)rect
{
// only draw our lines if we're not int he moddie of a transition and we
// acutally have some points to draw.
if(!self.hidden && nil != self.points && self.points.count > 0)
{
CGContextRef context = UIGraphicsGetCurrentContext();
if(nil == self.lineColor)
self.lineColor = [UIColor blueColor];
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetRGBFillColor(context, 0.8, 0.8, 0.8, 0.7);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 2.0);
for(int idx = 0; idx < self.points.count; idx++)
{
CLLocation* location = [self.points objectAtIndex:idx];
CGPoint point = [_mapView convertCoordinate:location.coordinate toPointToView:self];
if(idx == 0)
{
// move to the first point
CGContextMoveToPoint(context, point.x, point.y);
}
else
{
//CGContextAddQuadCurveToPoint(context, point.x, point.y, point.x, point.y);
CGContextAddLineToPoint(context, point.x, point.y);
}
}
CGContextFillPath(context);
}
}
#pragma mark mapView delegate functions
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
// turn off the view of the route as the map is chaning regions. This prevents
// the line from being displayed at an incorrect positoin on the map during the
// transition.
self.hidden = YES;
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
// re-enable and re-poosition the route display.
self.hidden = NO;
[self setNeedsDisplay];
}
-(void) dealloc
{
[_points release];
[_mapView release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment