原理:用google找到两个经纬度之间的路线点,然后在地图上把点用线连起来,就成了线路图
下面看代码:
currLocation = [[CLLocation alloc] initWithLatitude:34.82373560 longitude:113.67290260];
MKCoordinateRegion region;
region.center = currLocation.coordinate;
region.span = MKCoordinateSpanMake(0.1, 0.1);
MKCoordinateRegion adjustedRegion = [self.myMapView regionThatFits:region];
[self.myMapView setRegion:adjustedRegion animated:YES];
anno = [[BasicMapAnnotation alloc] initWithLatitude:currLocation.coordinate.latitude
andLongitude:currLocation.coordinate.longitude];
anno.title = @"当前位置";
[self.myMapView addAnnotation:anno];
annoDes = [[DestAnnotionView alloc] initWithLatitude:34.774621 andLongitude:113.700673];
annoDes.title = @"广播大学";
[self.myMapView addAnnotation:annoDes];
CLLocationCoordinate2D coordinates[2] = {{currLocation.coordinate.latitude, currLocation.coordinate.longitude}, {34.774621, 113.700673}};
locations1 = [self getPointsFrom:coordinates[0] toEnd:coordinates[1]];
NSLog(@"locations = %@",locations1);
[self drawLineWithLocationArray:locations1];
- (void)drawLineWithLocationArray:(NSArray *)locationArray
{
int pointCount = [locationArray count];
CLLocationCoordinate2D *coordinateArray = (CLLocationCoordinate2D *)malloc(pointCount * sizeof(CLLocationCoordinate2D));
for (int i = 0; i < pointCount; ++i) {
CLLocation *location = [locationArray objectAtIndex:i];
coordinateArray[i] = location.coordinate;
}
MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:pointCount];
[self.myMapView setVisibleMapRect:[routeLine boundingMapRect]];
[self.myMapView addOverlay:routeLine];
free(coordinateArray);
coordinateArray = NULL;
}
#pragma mark mapView delegate functions
-(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
routeimageView.hidden = YES;
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
[self updateRouteView:locations1];
routeimageView.hidden = NO;
[routeimageView setNeedsDisplay];
}
-(NSArray *)getPointsFrom:(CLLocationCoordinate2D)startPoint toEnd:(CLLocationCoordinate2D)destionPoint
{
NSString * start = [NSString stringWithFormat:@"%f,%f",startPoint.latitude,startPoint.longitude];
NSString * end = [NSString stringWithFormat:@"%f,%f",destionPoint.latitude,destionPoint.longitude];
NSString * path = [NSString stringWithFormat:@"http://maps.google.com/maps/api/directions/json?origin=%@&destination=%@&sensor=true",start,end];
ASIHTTPRequest * request1 = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:path]];
[request1 startSynchronous];
NSString * data1 = [request1 responseString];
NSDictionary * dic = [data1 objectFromJSONString];
NSArray * dicRouts = [dic valueForKey:@"routes"];
NSDictionary * legs = [dicRouts objectAtIndex:0];
NSDictionary * temp = [legs objectForKey:@"overview_polyline"];
NSMutableString * endpoints = (NSMutableString *)[temp objectForKey:@"points"];
NSArray * arr = (NSArray *)[self decodePolyLine:endpoints];
return arr;
}
-(NSMutableArray *)decodePolyLine:(NSMutableString *)encoded
{
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray * array1 = [[NSMutableArray alloc] init];
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];
NSNumber * longitude = [[NSNumber alloc] initWithFloat:lng*1e-5];
CLLocation * loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
[array1 addObject:loc];
}
NSLog(@"array1 = %@",array1);
return array1;
}
-(void)updateRouteView:(NSArray *)routs
{
CGContextRef context = CGBitmapContextCreate(nil, routeimageView.frame.size.width, routeimageView.frame.size.height, 8, 4*routeimageView.frame.size.width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.0);
CGContextSetLineWidth(context, 6.0);
for (int i=0; i<routs.count; i++)
{
CLLocation * location = [routs objectAtIndex:i];
CGPoint point = [self.myMapView convertCoordinate:location.coordinate toPointToView:routeimageView];
if (i==0)
{
CGContextMoveToPoint(context, point.x, routeimageView.frame.size.height-point.y);
}
else
{
CGContextAddLineToPoint(context, point.x, routeimageView.frame.size.height-point.y);
}
CGContextStrokePath(context);
CGImageRef image = CGBitmapContextCreateImage(context);
UIImage * image1 = [UIImage imageWithCGImage:image];
routeimageView.image = image1;
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BasicMapAnnotation class]])
{
MKAnnotationView * annocationView = [self.myMapView dequeueReusableAnnotationViewWithIdentifier:@"callAnno"];
if (annocationView == nil)
{
annocationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"callAnno"];
}
annocationView.image = [UIImage imageNamed:@"pin.png"];
annocationView.selected = YES;
annocationView.canShowCallout = YES;
return annocationView;
}
if ([annotation isKindOfClass:[DestAnnotionView class]])
{
MKAnnotationView * annocationView1 = [self.myMapView dequeueReusableAnnotationViewWithIdentifier:@"callAnno1"];
if (annocationView1 == nil)
{
annocationView1 = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"callAnno1"];
}
annocationView1.image = [UIImage imageNamed:@"pin.png"];
annocationView1.selected = YES;
annocationView1.canShowCallout = YES;
return annocationView1;
}
return nil;
}
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKPolylineView *view = [[MKPolylineView alloc] initWithOverlay:overlay];
view.strokeColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
NSArray *pattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:20], [NSNumber numberWithInt: 10], nil];
view.lineDashPattern = pattern;
view.lineWidth = 3;
return view;
}
有心人 慢慢看代码啊,不做过多的解释。