Corelocation and map Control learning notes

Source: Internet
Author: User

corelocation Basic Use

In the study of the map chapters, the first thing to learn is the location of the user, so we should first grasp the use of corelocation. (before IOS8 can directly request authorization, we need to notify the system to request authorization by our own call mode)

First, set a Corelocation property and implement the lazy load setting proxy, which requires itself to invoke methods Startupdatinglocation and stopupdatinglocation to start and end position fetching

 1  //  2  @property (nonatomic, strong) Cllocationmanager *manager;  3 -(Cllocationmanager *) manager{ 4  if  (_manager == nil) { 5  //  Create Corelocation manager  6  _manager = [[Cllocationmanager alloc] init]; 
7 _manager.delegate = self; 8 9 return _manager; Ten}

Set the Location Manager object and then make some common property configuration for its properties

1 //Configuring positioning Accuracy2 /*3 navigation level: kcllocationaccuracybestfornavigation;4 best: kcllocationaccuracybest;5 accurate to 10 meters: kcllocationaccuracynearesttenmeters;6 accurate to 100 meters: kcllocationaccuracyhundredmeters;7 accurate to 1000 meters: kcllocationaccuracykilometer;8 accurate to 3000 meters: kcllocationaccuracythreekilometers;9 */TenSelf.manager.desiredAccuracy =kcllocationaccuracybest; One //Update data after setting out of range, if not set data will be continuously updated ASelf.manager.distanceFilter = -;

If you use [self. Manager requestalwaysauthorization] method needs to add Nslocationalwaysusagedescription as key

1 //determine the system iOS version2     if([[Uidevice currentdevice].systemversion Doublevalue] >=8.0) {3NSLog (@"IOS8");4         //Note: After IOS8 need to verify authorization, in the Info.plist file also add nslocationwheninuseusagedescription this key,value can be empty, and call this method5 [Self.manager requestwheninuseauthorization];6 [Self.manager startupdatinglocation];7}Else{8NSLog (@"IOS7");9         //start getting user locationsTen [Self.manager startupdatinglocation]; One}

Next we need to implement some proxy methods to facilitate our access to the user location, the following is a common part of the proxy protocol

1. call when the user authorization status changes

1 //This determines the authorization status and does the corresponding operation2- (void) Locationmanager: (Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status{3     if(Status = = Kclauthorizationstatusauthorizedalways | |4Status = =kclauthorizationstatusauthorizedwheninuse) {5NSLog (@"Authorized Success");6         //start listening to get location after authorization is successful7 [Self.manager startupdatinglocation];8     }9}

2. Methods to invoke when updating user locations

1 //This is where you can get the location information from the anti-geocoding code .2- (void) Locationmanager: (Cllocationmanager *) ManagerDidupdatelocations: (Nsarray *) locations{3     //Anti -geocoding using location information anti-coding4Clgeocoder *geocoder =[[Clgeocoder alloc] init];5Cllocation *newlocation =[Locations Lastobject];6[Geocoder reversegeocodelocation:newlocation completionhandler:^ (Nsarray *placemarks, Nserror *error) {7          for(Clplacemark *placeinchplacemarks) {8             //User location information can be output via Clplacemark9NSLog (@"%@%@%lf%lf", Place.name, Place.addressdictionary, Place.location.coordinate.latitude,place.location.coordinate.longitude );Ten         } One     }]; A}

3. method to call when the user's phone head changes direction

1 // called when the user's head orientation is updated, the applicable direction specifies the action 2 -(void) Locationmanager: (Cllocationmanager *) managerdidupdateheading: (clheading *) Newheading

Now that the corelocation configuration is complete, here's how to configure the map

Basic use of the map control

First you import the map frame #import <MapKit/MapKit.h>

Set the map control properties and lazy load implementation and its property configuration

1 //map control, where the region attribute is more appropriate for the individual to set in the proxy2@property (nonatomic, strong) Mkmapview *Mapview;3-(Mkmapview *) mapview{4     if(_mapview = =Nil) {5_mapview =[[Mkmapview alloc] initWithFrame:self.view.bounds];6_mapview.Delegate=Self ;7         /*8 mkusertrackingmodenone = 0 default does not track9 Mkusertrackingmodefollow, track location.Ten mkusertrackingmodefollowwithheading tracking position and its head pointing One          */ A         //User tracking model, if not set, will not be able to track users -_mapview.usertrackingmode =mkusertrackingmodefollowwithheading; -         /* the mkmaptypestandard = 0, Normal mode (default mode) - Mkmaptypesatellite, satellite mode - Mkmaptypehybrid Blending Mode -          */ +_mapview.maptype =Mkmaptypestandard; -     } +     return_mapview; A}

Finally add the _mapview into the Self.view, and the entire map will be ready to load. Here is a partial proxy method for Mkmapview and its invocation time

1 #pragmaMark-mkmapviewdelegate Proxy method2 /**3 * Update user location is called4  *5 * @param mapview map6 * @param userlocation User Location7  */8- (void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation{9NSLog (@"Update user Location");Ten     //Set User Location center point One [Self.mapview setCenterCoordinate:userLocation.coordinate animated:yes]; A     //Range setting Mkcoordinatespanmake representation latitude and longitude range -Mkcoordinateregion region = Mkcoordinateregionmake (Userlocation.coordinate, Mkcoordinatespanmake (0.1,0.1)); -     //Set Center point range the [Self.mapview setregion:region animated:yes]; - } - /** - * Called when the map will start rendering +  */ -- (void) Mapviewwillstartrenderingmap: (Mkmapview *) mapview{ +NSLog (@"%s", __func__); A } at /** - * Called when the map finishes rendering -  */ -- (void) Mapviewdidfinishrenderingmap: (Mkmapview *) Mapview fullyrendered: (BOOL) fullyrendered{ -NSLog (@"%s", __func__); - } in /** - * Called when region (range) is about to change to  */ +- (void) Mapview: (Mkmapview *) Mapview regionwillchangeanimated: (BOOL) animated{ -NSLog (@"%s", __func__); the } * /** $ * Called when region (range) has completed a changePanax Notoginseng  */ -- (void) Mapview: (Mkmapview *) Mapview regiondidchangeanimated: (BOOL) animated{ theNSLog (@"%s", __func__); + } A /** the * Call when tracking model changes +  */ -- (void) Mapview: (Mkmapview *) Mapview Didchangeusertrackingmode: (mkusertrackingmode) mode animated: (BOOL) animated{ $NSLog (@"%s", __func__); $ } - /** - * When setting a PIN, call similar to Tableviewcell setting the  */ --(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (ID<MKAnnotation>) annotation{
51//In which Mkannotationview is set, there is a reuse mechanism
32;

In addition, there are some other proxy methods are not introduced, the rest depends on the corresponding method to add the action you want to do.

Corelocation and map Control learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.