////VIEWCONTROLLER.M//Mapdrawline////Created by Cheze on 2016/10/27.//Copyright 2016 Xiezefeng. All rights reserved.//#import "ViewController.h"#import<MapKit/MapKit.h>@interfaceViewcontroller () <MKMapViewDelegate>@property (nonatomic, strong) Mkpolyline*Routeline, @property (nonatomic, strong) Nsmutablearray*Mutarray, @property (nonatomic, strong) Clgeocoder*Geocoder, @property (nonatomic,strong) Mkmapview*Mapview;@end@implementationViewcontroller-(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.4Longitude: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]; //additional setup after loading the view, typically from a nib.}-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ Doublenum =0.1* (Arc4random ()%Ten); DoubleNUM1 =0.1* (Arc4random ()%Ten); 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];}//Ground Drawing Line- (void) Drawlinewithlocationarray: (Nsmutablearray *) locationarray{Mkmappoint* Pointarray = malloc (sizeof(cllocationcoordinate2d) *locationarray.count); for(intIDX =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; returnrenderer; }- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Map Mapkit Plot route