首页 > 代码库 > 地图Mapkit 绘制路线
地图Mapkit 绘制路线
// // ViewController.m // MapDrawLine // // Created by 谢泽锋 on 2016/10/27. // Copyright ? 2016年 xiezefeng. All rights reserved. // #import "ViewController.h" #import <MapKit/MapKit.h> @interface ViewController ()<MKMapViewDelegate> @property (nonatomic, strong) MKPolyline *routeLine; @property (nonatomic, strong) NSMutableArray *mutArray; @property (nonatomic, strong) CLGeocoder *geocoder; @property(nonatomic,strong)MKMapView * mapView; @end @implementation ViewController - (NSMutableArray *)mutArray { if (_mutArray == nil) { _mutArray = [NSMutableArray array]; } return _mutArray; } - (CLGeocoder *)geocoder { if (!_geocoder) { self.geocoder = [[CLGeocoder alloc] init]; } return _geocoder; } - (void)viewDidLoad { [super viewDidLoad]; _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*2/3)]; _mapView.delegate= self; CLLocation *location1 = [[CLLocation alloc] initWithLatitude:23.4 longitude:113.3]; // CLLocation *location2 = [[CLLocation alloc] initWithLatitude:23.6 longitude:113.9]; // CLLocation *location3 = [[CLLocation alloc] initWithLatitude:23.0 longitude:113.4]; // CLLocation *location4 = [[CLLocation alloc] initWithLatitude:23.7 longitude:113.7]; [self.mutArray addObject:location1]; // [self.mutArray addObject:location2]; // [self.mutArray addObject:location3]; // [self.mutArray addObject:location4]; [self.view addSubview:_mapView]; MKCoordinateRegion region; // region.span = MKCoordinateSpanMake(1, 1); region.center = CLLocationCoordinate2DMake(location1.coordinate.latitude,location1.coordinate.longitude); [self.mapView setRegion:region animated:YES]; [self drawLineWithLocationArray:self.mutArray]; // Do any additional setup after loading the view, typically from a nib. } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ double num = 0.1*(arc4random()%10); double num1 =0.1*(arc4random()%10); CLLocation *location4 = [[CLLocation alloc] initWithLatitude:23.7+num longitude:113.7+num1]; [self.mutArray addObject:location4]; [self drawLineWithLocationArray:self.mutArray]; } - (IBAction)ac:(id)sender { [self.mutArray removeAllObjects]; [self drawLineWithLocationArray:self.mutArray]; } //地图画线 - (void)drawLineWithLocationArray:(NSMutableArray *)locationArray { MKMapPoint* pointArray = malloc(sizeof(CLLocationCoordinate2D) * locationArray.count); for(int idx = 0; idx < locationArray.count; idx++) { CLLocation *location = [locationArray objectAtIndex:idx]; CLLocationDegrees latitude = location.coordinate.latitude; CLLocationDegrees longitude = location.coordinate.longitude; CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude); MKMapPoint point = MKMapPointForCoordinate(coordinate); pointArray[idx] = point; } if (_routeLine) { [_mapView removeOverlay:_routeLine]; } _routeLine = [MKPolyline polylineWithPoints:pointArray count:locationArray.count]; if (nil != _routeLine) { [_mapView addOverlay:_routeLine]; } free(pointArray); } -(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{ MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay]; renderer.strokeColor = [UIColor blueColor]; renderer.lineWidth = 2; return renderer; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
地图Mapkit 绘制路线
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。