iOS 在地圖上繪制行程路線

在上篇文章得到了一個polyline數(shù)組,例如:
**2016-02-05 16:21:27.928 ****字符串轉(zhuǎn)Polyline數(shù)組****[3707:51509] 31.214199,121.535149**
**2016-02-05 16:21:27.929 ****字符串轉(zhuǎn)Polyline數(shù)組****[3707:51509] 31.214199,121.535149**
**2016-02-05 16:21:27.929 ****字符串轉(zhuǎn)Polyline數(shù)組****[3707:51509] 31.214211,121.535141**
**2016-02-05 16:21:27.929 ****字符串轉(zhuǎn)Polyline數(shù)組****[3707:51509] 31.214211,121.535141**
**2016-02-05 16:21:27.929 ****字符串轉(zhuǎn)Polyline數(shù)組****[3707:51509] 31.214211,121.535149**
**2016-02-05 16:21:27.929 ****字符串轉(zhuǎn)Polyline數(shù)組****[3707:51509] 31.214211,121.535149**
.
.
.
數(shù)組總共有389個元素,都是經(jīng)緯度...
我們把這個數(shù)組稱為:polylint
地圖繪制需要遵循一個代理:<MKMapViewDelegate>

代碼如下:
@interface ViewController ()<MKMapViewDelegate>

@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數(shù)組
    
    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];
   
}

//實現(xiàn)畫線的代理方法
- (MKPolylineRenderer *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)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;
        
    }
    
    
}
234F9992-E697-4B67-A7B9-5ECF2A9D41F1.png
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容