MKMapView类——获取并绘制两点之间的路线
创建View based工程文件,导入框架MapKit.framework CoreGraphics.framework libicucore.dylib(重要)。
详见源码下载:https://files.cnblogs.com/wujian1360/TheMapRoute_wujain.zip
- (id) initWithFrame:(CGRect) frame
{
self = [super initWithFrame:frame];
if (self) {
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
mapView.showsUserLocation = YES;
[mapView setDelegate:self];
[self addSubview:mapView];
routeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];
routeView.userInteractionEnabled = NO;
[mapView addSubview:routeView];
self.lineColor = [UIColor redColor];
}
return self;
}
//显示两点之间的线路
-(void) showRouteFrom: (Place*) f to:(Place*) t {
if(routes) {
[mapView removeAnnotations:[mapView annotations]];
[routes release];
}
PlaceMark* from = [[[PlaceMark alloc] initWithPlace:f] autorelease];
PlaceMark* to = [[[PlaceMark alloc] initWithPlace:t] autorelease];
[mapView addAnnotation:from];
[mapView addAnnotation:to];
//计算出两点之间的线路所包含的点的数组
routes = [[self calculateRoutesFrom:from.coordinate to:to.coordinate] retain];
//根据数组画线
[self updateRouteView];
//根据线路确定呈现范围
[self centerMap];
}
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];
NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSLog(@"\n api url: %@", apiUrl);
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:nil];
NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];
NSLog(@"\n %@",encodedPoints);
return [self decodePolyLine:[encodedPoints mutableCopy]];
}
/*
{tooltipHtml:" (23.4\x26#160;km / 25 mins)",
polylines:[{id:"route0",
points:"{ntrFopdfU?rv@ZrcB??UzALrE??Ot@@bG??e@\\oF?cCp@u@???gCf@yu@C??wuAEaKHkJb@eHx@eGrA_EpAyHrDyEdDcGtFyErF_J|Mgb@dt@??yfB||Cw@bB}ApEcLv`@uBnIcAdGOjBS|Gt@bhArA`yA??rBd~BAdOHxJb@rQDhLJpD|ArkB^fWrA|sAdApm@??v@na@jCpjC??m@nDXvQ??lH_@??BbA??_@V",
levels:"B?BB?BB?BB???BB?BB?@???A???@?BB@???A??@?BB?????????BB?BB?BBBBBBB",
numLevels:4,zoomFactor:16}]}
{ntrFopdfU?rv@ZrcB??UzALrE??Ot@@bG??e@\\oF?cCp@u@???gCf@yu@C??wuAEaKHkJb@eHx@eGrA_EpAyHrDyEdDcGtFyErF_J|Mgb@dt@??yfB||Cw@bB}ApEcLv`@uBnIcAdGOjBS|Gt@bhArA`yA??rBd~BAdOHxJb@rQDhLJpD|ArkB^fWrA|sAdApm@??v@na@jCpjC??m@nDXvQ??lH_@??BbA??_@V
*/
////解码算法(将获取的字符串转码为地图上的坐标位置,并以此构建每一个CLLocation位置对象,然后加入数组 /////////////////////
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
//替换字符串中的@"\\" 为 @"\\\\" ,检索方式为逐个字符,范围是整个字符串
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
printf("[%f,", [latitude doubleValue]);
printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];
[array addObject:loc];
}
return array;
}
//用routes数组的内容 确定region的呈现范围
-(void) centerMap {
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx < routes.count; idx++)
{
CLLocation* currentLocation = [routes 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;
}
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;
[mapView setRegion:region animated:YES];
}
//用routes数组的内容 画线
-(void) updateRouteView {
CGContextRef context = CGBitmapContextCreate(nil,
routeView.frame.size.width,
routeView.frame.size.height,
8,
4 * routeView.frame.size.width,
CGColorSpaceCreateDeviceRGB(),
kCGImageAlphaPremultipliedLast);
CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 3.0);
for(int i = 0; i < routes.count; i++) {
CLLocation* location = [routes objectAtIndex:i];
CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:routeView];
if(i == 0) {
CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y);
} else {
CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y);
}
}
CGContextStrokePath(context);
CGImageRef image = CGBitmapContextCreateImage(context);
UIImage* img = [UIImage imageWithCGImage:image];
routeView.image = img;
CGContextRelease(context);
}
#pragma mark mapView delegate functions
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
routeView.hidden = YES;
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
[self updateRouteView];
routeView.hidden = NO;
[routeView setNeedsDisplay];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
if(routes) {
[routes release];
}
[mapView release];
[routeView release];
[super dealloc];
}
#import <Foundation/Foundation.h>
@interface Place : NSObject {
NSString* name;
NSString* description;
double latitude;
double longitude;
}
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSString* description;
@property (nonatomic) double latitude;
@property (nonatomic) double longitude;
@end
#import "Place.h"
@implementation Place
@synthesize name;
@synthesize description;
@synthesize latitude;
@synthesize longitude;
- (void) dealloc
{
[name release];
[description release];
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "Place.h"
@interface PlaceMark : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
Place* place;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) Place* place;
-(id) initWithPlace: (Place*) p;
@end
#import "PlaceMark.h"
@implementation PlaceMark
@synthesize coordinate;
@synthesize place;
-(id) initWithPlace: (Place*) p
{
self = [super init];
if (self != nil) {
coordinate.latitude = p.latitude;
coordinate.longitude = p.longitude;
self.place = p;
}
return self;
}
- (NSString *)subtitle
{
return self.place.description;
}
- (NSString *)title
{
return self.place.name;
}
- (void) dealloc
{
[place release];
[super dealloc];
}
@end