iOS 在地图上绘制行程路线

在上篇文章得到了一个polyline数组,例如:
**2016-02-05 16:21:27.928 ****字符串转Polyline数组****[3707:51509] 31.214199,121.535149**
**2016-02-05 16:21:27.929 ****字符串转Polyline数组****[3707:51509] 31.214199,121.535149**
**2016-02-05 16:21:27.929 ****字符串转Polyline数组****[3707:51509] 31.214211,121.535141**
**2016-02-05 16:21:27.929 ****字符串转Polyline数组****[3707:51509] 31.214211,121.535141**
**2016-02-05 16:21:27.929 ****字符串转Polyline数组****[3707:51509] 31.214211,121.535149**
**2016-02-05 16:21:27.929 ****字符串转Polyline数组****[3707:51509] 31.214211,121.535149**
.
.
.
数组总共有389个元素,都是经纬度...
我们把这个数组称为:polylint
地图绘制需要遵循一个代理:

代码如下:
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    MKMapView *view = [[MKMapView alloc]initWithFrame:CGRectMake(0 , 0, 375, 667)];
    [self.view addSubview:view];
    view.delegate = self;
    
    NSArray *polylint = 得到的polyline数组
    
    NSInteger pointCount = polylint.count;
    
    CLLocationCoordinate2D *coordinateArray = (CLLocationCoordinate2D *)malloc(pointCount * sizeof(CLLocationCoordinate2D));
    
    for (int i = 0; i < pointCount; i++)
    {
        CLLocation *location = [polylint objectAtIndex:i];
        coordinateArray[i] = [location coordinate];
        NSLog(@"%d",i);
        NSLog(@"%f,%f",location.coordinate.latitude,location.coordinate.longitude);
        
       
        
    }
    
    MKPolyline *lines = [MKPolyline polylineWithCoordinates:coordinateArray count:pointCount];
     [view addOverlay:lines];
    MKCoordinateSpan span ={0.03,0.04};
    MKCoordinateRegion regon = MKCoordinateRegionMake(coordinateArray[0], span);
    [view setRegion:regon animated:YES];
   
}

//实现画线的代理方法
- (MKPolylineRenderer *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay{
    
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        
        MKPolylineRenderer *line = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        line.strokeColor = [UIColor blackColor];
        line.lineWidth = 3.5f;
        return line;
        
    }else{
        
        return nil;
        
    }
    
    
}
iOS 在地图上绘制行程路线_第1张图片
234F9992-E697-4B67-A7B9-5ECF2A9D41F1.png

你可能感兴趣的:(iOS 在地图上绘制行程路线)