地图描线的简单示例
首先,需要将CoreLocation.framework和MapKit.framework加入到项目中。
?
?
mapLinesViewController.h
?
#import <UIKit/UIKit.h>#import <MapKit/MapKit.h>#import "MapRouteLayerView.h"@interface mapLinesViewController : UIViewController {MKMapView *mapView;MapRouteLayerView *routeView;}@property(nonatomic, retain) MKMapView *mapView;@property(nonatomic, retain) MapRouteLayerView *routeView;@end
?
mapLinesViewController.m
?
#import "mapLinesViewController.h"#import <CoreLocation/CoreLocation.h>#import "MapRouteLayerView.h"@implementation mapLinesViewController@synthesize routeView;@synthesize mapView;- (void)viewDidLoad { [super viewDidLoad];NSString *filePath = [[NSBundle mainBundle] pathForResource:@"route" ofType:@"csv"];NSString *fileContents = [NSString stringWithContentsOfFile:filePath];NSArray *pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];NSMutableArray *points = [[NSMutableArray alloc] initWithCapacity:pointStrings.count];for(int idx = 0; idx < pointStrings.count; idx++) {NSString *currentPointString = [pointStrings objectAtIndex:idx];NSArray *latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue];CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];CLLocation *currentLocation = [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease];[points addObject:currentLocation];}mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];[self.view addSubview:mapView];routeView = [[MapRouteLayerView alloc] initWithRoute:points mapView:mapView];[points release];}- (void)viewDidUnload {self.mapView = nil;self.routeView = nil;}- (void)dealloc {[mapView release];[routeView release]; [super dealloc];}@end
?
MapRouteLayerView.h
?
#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface MapRouteLayerView : UIView <MKMapViewDelegate> {MKMapView *mapView;NSArray *points;UIColor *lineColor;}@property(nonatomic, retain) NSArray *points;@property(nonatomic, retain) MKMapView *mapView;@property(nonatomic, retain) UIColor *lineColor; - (id)initWithRoute:(NSArray *)routePoints mapView:(MKMapView *)mapView;@end
?
MapRouteLayerView.m
?
#import "MapRouteLayerView.h"@implementation MapRouteLayerView@synthesize mapView;@synthesize points;@synthesize 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];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.mapView setRegion:region];[self.mapView setDelegate:self];[self.mapView addSubview:self];return self;}- (void)drawRect:(CGRect)rect {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.0, 0.0, 1.0, 1.0);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) {CGContextMoveToPoint(context, point.x, point.y);} else {CGContextAddLineToPoint(context, point.x, point.y);}}CGContextStrokePath(context);}}#pragma mark mapView delegate functions- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {self.hidden = YES;}- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {self.hidden = NO;[self setNeedsDisplay];}- (void) dealloc {[points release];[mapView release];[lineColor release];[super dealloc];}@end
?
示例图: