首先要说明的是这两个类是完成划线的基础类.利用它们可以实现我们想要的效果
1.MKPolyline :用于地图上面划线的类
2.MKPolylineView:用于展示线的视图
下面是实现画一组坐标点之间的划线
#import <MapKit/MapKit.h>
//遵循代理协议<MKMapViewDelegate>
//地图对象
@property (nonatomic, strong) MKMapView *mapView;
//1@property (nonatomic, strong) MKPolyline *routeLine;//2
@property (nonatomic, strong) MKPolylineView *routeLineView;
//覆盖---就是绘画的直线- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{ if(overlay == self.routeLine) { if(nil == self.routeLineView) { self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine]; self.routeLineView.fillColor = [UIColor redColor]; self.routeLineView.strokeColor = [UIColor cyanColor]; self.routeLineView.lineWidth = 5; } return self.routeLineView; } return nil;}
#pragma mark ---开始绘画//开始绘画- (void)drawTestLine{ //创建一个可变数组,用来存放CLLocation对象的所有数组 _array = [NSMutableArray array]; for (int i = 0; i < _pointArray.count; i ++) { PictureImageModel *model = _pointArray[i];#warning ========= //坐标 CLLocation *loca = [[CLLocation alloc] initWithLatitude:model.latitude longitude:model.longitude]; [_array addObject:loca]; } //调用两点之间的连线 [self drawLineWithLocationArray:_array];}#warning ------//绘画的数组- (void)drawLineWithLocationArray:(NSArray *)locationArray{ //获取传过来的数组的个数 NSInteger 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]; } self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:pointCount]; [self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; [self.mapView addOverlay:self.routeLine]; free(coordinateArray); coordinateArray = NULL; }
转载于:https://www.cnblogs.com/ryc--520/articles/5037147.html
相关资源:微信小程序地图实现展示线路
转载请注明原文地址: https://win8.8miu.com/read-1558668.html