1.CoreLocation use, get current position

Source: Internet
Author: User

1.

iOS7 the system will automatically ask you to authorize your application as soon as you start positioning

after iOS8 , the code must be implemented to require the user to authorize the application, adding the following two properties in Plist

Nslocationwheninusedescription, allowing GPS descriptions to be obtained at the front desk

Nslocationalwaysusagedescription, allows the description of GPS to be acquired in the background

2, Code

#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

@interface Viewcontroller () <cllocationmanagerdelegate>

@property(nonatomic,strong)Cllocationmanager*manger;

@end

@implementation Viewcontroller

-(cllocationmanager*) Manger

{

if (_manger= =Nil) {

_manger=[[cllocationmanager alloc]init];

}

return _manger;

-(void) viewdidload {

[Super viewdidload];

self. Manger. Delegate=self;

if ([[uidevice currentdevice]. Systemversion floatvalue]>8.0) {

[self. Manger requestalwaysauthorization]; Perform get location information when running in the background

//[self.manger requestwheninuseauthorization];// Get location information when the front desk executes

}

Else

{

[self. Manger startupdatinglocation];

}

}

call when the authorization status changes

-(void) Locationmanager: (cllocationmanager *) Manager didchangeauthorizationstatus: ( Clauthorizationstatus) Status

{

if (status = = kclauthorizationstatusnotdetermined) {

NSLog(@ " waiting for user authorization ");

}Else if (status = = Kclauthorizationstatusauthorizedalways | |

Status = = kclauthorizationstatusauthorizedwheninuse)

{

NSLog(@ " Authorized success ");

// start positioning

[self. Manger startupdatinglocation];

}

Else

{

NSLog(@ " authorization failed ");

}

}

-(void) Locationmanager: (cllocationmanager *) Manager didupdatelocations: (nsarray *) Locations

{

// If you only need to get once , you can get the location and then stop

[self. Manger stopupdatinglocation];

cllocation *location=[locations lastobject];

NSLog(@ " longitude =%f, dimension =%f speed =%f", Location. Coordinate. Latitude, location. Coordinate. Longitude, location. Speed);

}

@end

1.CoreLocation use, get current position

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.