美文网首页iOS开发iOS DeveloperiOS 开发
iOS 在地图上绘制行程路线

iOS 在地图上绘制行程路线

作者: 找不到好的ID | 来源:发表于2016-02-15 11:19 被阅读1478次
在上篇文章得到了一个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
地图绘制需要遵循一个代理:<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数组
    
    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<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

相关文章

网友评论

  • Shawn_Wang:求问 地图上的地铁路线是怎么出来的 :blush:
    找不到好的ID:@Shawn_Wang 你用模拟器试一下.
    Shawn_Wang:@number3 我试了没有哎。微博给你私信了。方便查看下吗?这里没法贴图。。
    找不到好的ID:@Shawn_Wang 原生地图自带的

本文标题:iOS 在地图上绘制行程路线

本文链接:https://www.haomeiwen.com/subject/walpkttx.html