標籤:copyright rand enter esb with iba reg gre dex
//// 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 繪製路線