The core location for iOS development

Source: Internet
Author: User

IOS supports three ways to detect your current location: Mobile phone base station, Wi-Fi, and GPS, where GPs is the highest longitude, and also the most expensive phone charge. Generally in the indoor is not able to obtain location information through the GPS, the principle of acquiring location via Wi-Fi through the network provider's IP address information to obtain the location, longitude is not very high, finally through the mobile phone base station to obtain the location, mobile phone will connect the nearby base station tower to obtain the signal, Through the base station can get the location information of the mobile phone, the denser the base station, the higher the longitude of the location information obtained.

The core location provided by the IOS SDK provides better access to location information, which involves the following classes, Cllocationmanager (location manager), Cllocation, Cllocationmanagerdelegate (protocol, provide delegate method), cllocationcoodinate2d (store coordinate location)

In addition, there are several properties of Cllocationmanager;

Desiredaccuracy: Positional Precision Properties

There are several values:

Kcllocationaccuracybest

Best accuracy

Kcllocationaccuracynearesttenmeters

Accuracy within 10m

Kcllocationaccuracyhundredmeters

Accuracy within 100m

Kcllocationaccuracykilometer

Accuracy within 1000m

Kcllocationaccuracythreekilometers

Accuracy within 3000m

Distancefilter: Update location information After how many distances are moved sideways

Delegate: Object responding to Cllocationmanagerdelegate

Here's an example of getting a location:

First create a single View application project, then introduce Corelocation.framework, and modify the following in ViewController.h:

#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h>
@interface viewcontroller:uiviewcontroller<cllocationmanagerdelegate> {Cllocationmanager *locManager;}
@property (Retain, nonatomic) Iboutletuilabel *lonlabel; @property (Retain, nonatomic) Iboutletuilabel *latlabel; @property (Retain, nonatomic) Cllocationmanager *locmanager;
@end
The implementation of the. m file is as follows, as explained in the code comment#import"ViewController.h"
@interfaceViewController ()
@end
@implementation Viewcontroller@synthesize Lonlabel;@synthesize Latlabel;@synthesize Locmanager;
- (void) Viewdidload {[Superviewdidload];//Initializing the location ManagerLocmanager = [[Cllocationmanager alloc]init];//Set up ProxyLocmanager.delegate = self;//Set Location LongitudeLocmanager.desiredaccuracy = Kcllocationaccuracybest;//Set update location every 100 metersLocmanager.distancefilter =100;//Start Location Services[Locmanagerstartupdatinglocation]; }
- (void) Viewdidunload {[Selfsetlonlabel:nil];     [Selfsetlatlabel:nil]; [Superviewdidunload];//Release any retained subviews of the main view.}
-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation {Return (interfaceorientation! = Uiinterfaceorientationportraitupsidedown); }
- (void) Dealloc {//Stop Location Services[Locmanagerstopupdatinglocation];     [Lonlabelrelease];     [Latlabelrelease]; [Superdealloc]; }
//The method in the protocol, which is the delegate method that is called whenever a location update occurs-(void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation *) newlocation fromlocation: ( Cllocation *) Oldlocation {//structure, storage location coordinatesCllocationcoordinate2d loc = [newlocation coordinate];float longitude = loc.longitude;float latitude = loc.latitude; Self.lonLabel.text = [Nsstringstringwithformat:@"%f", longitude]; Self.latLabel.text = [Nsstringstringwithformat:@"%f", Latitude]; }
//Method that is called when a location fetch or update fails-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) Error {NSString *errormsg = nil;if ([error code] = = kclerrordenied) {errormsg =@"Access is denied"; }if ([error code] = = Kclerrorlocationunknown) {errormsg =@"Failed to get location information"; } Uialertview *alertview = [[Uialertviewalloc]initwithtitle:@ "location" message:errormsg delegate:self cancelbuttontit     Le:@ "OK" otherbuttontitles:nil, nil];     [Alertview show]; [Alertview release]; }
@end
The final compile and run results are as follows (the latitude and longitude obtained on the simulator):

The core location for iOS development

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.