The use of iOS maps is explained in detail in this code:
Step one: Add a frame (although you don't know when to add a frame, Xcode will automatically add it to us, but let's add it)
Step Two: Configure the Pliset file
Step three: Directly on the code block, be sure to read carefully
#import "ViewController.h"//import two frames for positioning and maps #import <CoreLocation/CoreLocation.h> #import <mapkit/mapkit.h >//signed the proxy agreement for positioning and Mapping @interface Viewcontroller () <cllocationmanagerdelegate,mkmapviewdelegate>// Location Manager @property (nonatomic, strong) Cllocationmanager *localmanager;//map @property (nonatomic, strong) MKMapView * mapview;//an array of user locations @property (nonatomic, strong) Nsmutablearray *locationmutablearray; @end @implementation Viewcontroller#pragma Mark-Position manager Lazy Loading-(Cllocationmanager *) localmanager{if (_localmanager = = nil) {_localm Anager = [[Cllocationmanager alloc]init]; Set the accuracy of positioning [_localmanager setdesiredaccuracy:kcllocationaccuracybest]; Location information update minimum distance _localmanager.distancefilter = 10; Set proxy _localmanager.delegate = self; If no authorization is requested, the user is authorized,//Because Requestalwaysauthorization is proposed after iOS8, it is necessary to add a condition to be able to respond to the judgment, to prevent crashes if ([Cllocationmanager AUT Horizationstatus]==kclauthorizationstatusnotdeterminEd && [_localmanager respondstoselector: @selector (requestalwaysauthorization)]) {[_localmanager reques Talwaysauthorization]; }//Create an array of storage locations _locationmutablearray = [[Nsmutablearray alloc] init]; } return _localmanager;} -(void) viewdidload{[Super Viewdidload]; Display the map in full screen and set the proxy for the map _mapview = [[Mkmapview alloc] Initwithframe:[uiscreen mainscreen].bounds]; _mapview.delegate = self; Whether to enable Location services if ([Cllocationmanager locationservicesenabled]) {NSLog (@ "start positioning"); When the Startupdatinglocation method is called, it corresponds to the Didupdatelocations method [Self.localmanager startupdatinglocation]; } else{NSLog ("Location service is off, unable to use location services"); }//USER location tracking _mapview.usertrackingmode = Mkusertrackingmodefollow; Style of the/** map: mkmaptypestandard, Standard map mkmaptypesatellite, satellite map Mkmaptypehybrid, mixed map MKMAPTYPESATELLITEFL Yover, Satellite stereoscopic map Mkmaptypehybridflyover, mixed stereoscopic map */_mapview.maptype = MkmapTypestandard; [Self.view Addsubview:_mapview];} #pragma mark-mkmapviewdelegate/** Update the user location, call this method (including the first time to navigate to the user's location) when the user changes the first way to draw the trajectory: we use the changes on the map to describe the trajectory, this way without considering the Cllocationmanager the latitude and longitude taken out of the Mapview shows a deviation problem */-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: ( Mkuserlocation *) userlocation{nsstring *latitude = [NSString stringwithformat:@ "%3.5f", UserLocation.coordinate.lat Itude]; NSString *longitude = [NSString stringwithformat:@ "%3.5f", UserLocation.coordinate.longitude]; NSLog (@ "Updated user location: Latitude:%@, Longitude:%@", latitude,longitude); Set the map display range (if no locale is automatically displayed and specify the current user location as the Map center point) Mkcoordinatespan span = Mkcoordinatespanmake (0.05, 0.05); Mkcoordinateregion Region=mkcoordinateregionmake (userLocation.location.coordinate, span); [_mapview setregion:region animated:true]; if (_locationmutablearray.count! = 0) {//Remove the latest position data from the position array nsstring *locationstr = _locationmutablearray.last Object; Nsarray *temp = [Locationstr componentsseparatedbystring:@ ","]; NSString *latitudestr = temp[0]; NSString *longitudestr = temp[1]; Cllocationcoordinate2d startcoordinate = Cllocationcoordinate2dmake ([latitudestr doublevalue], [longitudeStr Doublevalue]); Location data currently determined to cllocationcoordinate2d Endcoordinate; Endcoordinate.latitude = UserLocation.coordinate.latitude; Endcoordinate.longitude = UserLocation.coordinate.longitude; Calculation of the moving distance double meters = [self calculatedistancewithstart:startcoordinate end:endcoordinate]; NSLog (@ "Moving distance is%f m", meters); In order to beautify the moving trajectory, move the position more than 10 meters, the square can be added into the position of the array if (meters >= 0) {NSLog (@ "add into the position array"); NSString *locationstring = [NSString stringwithformat:@ "%f,%f", UserLocation.coordinate.latitude, UserLocation.coordinate.longitude]; [_locationmutablearray addobject:locationstring]; Start drawing trajectory cllocationcoordinate2d pointstouse[2]; POINTSTOUSE[0] = Startcoordinate; POINTSTOUSE[1] = endcoordinate; After calling the Addoverlay method, the Rendererforoverlay method is entered, and the trajectory is drawn mkpolyline *lineone = [Mkpolyline polylinewithcoordinates :p Ointstouse Count:2]; [_mapview Addoverlay:lineone]; }else{NSLog (@ "Do not add into position array"); }}else{//An array of stored positions, if the array contains 0 objects, then the first entry is entered, the current position is added to the position array nsstring *locationstring = [NSString string withformat:@ "%f,%f", UserLocation.coordinate.latitude, UserLocation.coordinate.longitude]; [_locationmutablearray addobject:locationstring]; }}-(Mkoverlayrenderer *) Mapview: (Mkmapview *) Mapview Rendererforoverlay: (id<mkoverlay>) overlay{if ([overlay Iskindofclass:[mkpolyline class]] {#pragma clang diagnostic push#pragma clang diagnostic ignored "- Wdeprecated-declarations "Mkpolylineview *polylineview = [[Mkpolylineview alloc] initwithpolyline:overlay]; Polylineview.linewidth = 10; Polyline width PolylineviEw.strokecolor = [Uicolor Bluecolor]; Line Color return (Mkoverlayrenderer *) Polylineview; #pragma clang diagnostic pop} return nil;} #pragma mark-cllocationmanagerdelegate/** * When the current location authorization status changes, call * * @param manager Location Manager * @param status authorization State */-(void) Locationmanager: (Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status{switch ( Status) {case kclauthorizationstatusnotdetermined:{NSLog (@ "User is not authorized"); Break } Case kclauthorizationstatusdenied:{//determine if the current device supports the location and location service to open if ([Cllocationmanager Servicesenabled]) {NSLog (@ "user does not allow the program to access location information or manually turn off access to location information, to help jump to the Settings interface"); Nsurl *url = [Nsurl urlwithstring:uiapplicationopensettingsurlstring]; if ([[[UIApplication sharedapplication] canopenurl:url]) {[[UIApplication sharedapplication] Openurl:u RL]; }}else{ NSLog (@ "Location service off, pop-up System prompt box, click Settings can jump to the location service interface to open the location service"); } break; } Case kclauthorizationstatusrestricted:{NSLog (@ "restricted"); Break } Case kclauthorizationstatusauthorizedalways:{NSLog (@ "authorized to use location services in the foreground and backstage"); Break } Case kclauthorizationstatusauthorizedwheninuse:{NSLog (@ "authorized to use location services at the front desk"); Break } Default:break; }}/** we did not put the latitude and longitude from the Cllocationmanager on the Mapview. The reason: the latitude and longitude that we take in this method is based on the earth coordinates, but the domestic map shows that the standard is Mars coordinates Mkmapview Do not do any of the processing, is because Mkmapview is already processed and causes the coordinates obtained in this method to be shown on the Mapview is biased solution There are many kinds of solutions can be online query, here will not do more repeat */-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{//device's current location cllocation *currLoc ation = [Locations Lastobject]; NSString *latitude = [NSString stringwithformat:@ "Latitude:%3.5f", currLocation.coordinate.latitude]; NSString *longitude = [NSString stringwithformat:@ "Longitude:%3.5f", currLocation.coordinate.longitude]; NSString *altitude = [nsstring stringwithformat:@ "Height value:%3.5f", currlocation.altitude]; NSLog (@ "position changed: Latitude:%@, Longitude:%@, Height:%@", latitude,longitude,altitude); [Manager stopupdatinglocation];} Locate failed callback method-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error{NSLog (@ "Cannot get current position Error:%@ ", error.localizeddescription);} #pragma mark-distance measurement-(double) Calculatedistancewithstart: (cllocationcoordinate2d) Start end: (cllocationcoordinate2d) End {Double meter = 0; Double startlongitude = start.longitude; Double startlatitude = start.latitude; Double endlongitude = end.longitude; Double endlatitude = end.latitude; Double radLatitude1 = startlatitude * m_pi/180.0; Double RadLatitude2 = endlatitude * m_pi/180.0; Double A = fabs (Radlatitude1-radlatitude2); Double b = fabs (Startlongitude * m_pi/180.0-endlongitude * m_pi/180.0); Double s = 2 * ASIN (sqrt (Pow (sin (A/2), 2) + cos (radLatitude1) * cos (radLatitude2) * POW (sin (b/2), 2))); s = S * 6378137; Meter = Round (S * 10000)/10000; return meter;} @end
The code explains in detail the use of the map and the circumstances of the real machine, the location of the map, displacement is often used in the function, you can use the above code to get started, better help you with the development of the map function, code implementation and real-time mapping of the function of the trajectory, Of course, the real-time mapping of the function is not perfect, and now Baidu also launched the Eagle Eye map, you can view their trajectory information. Development is the choice of the most appropriate method to complete the functional requirements.
Code: http://download.csdn.net/detail/wuzesong/9575926
Use of iOS maps and real-time depiction of motion trajectories