IOS Study Notes 17 -- core location

Source: Internet
Author: User

IOS supports three methods to detect the current location: mobile phone base station, Wi-Fi, and GPS. GPS is the highest longitude and consumes the most mobile phone power. Generally, location information cannot be obtained through GPS in the room. Wi-Fi is used to obtain the location information through the IP address of the network provider. The longitude is not very high, the last step is to obtain the location through the mobile phone base station. After the mobile phone is started, it connects to the nearby base station tower to obtain the signal. The base station can obtain the location information of the mobile phone. The more intensive the base station is, the longitude of the obtained location information is higher.


The core location provided by ios sdk provides a better function for obtaining location information. The following categories are involved in obtaining location information: cllocationmanager, cllocation, cllocationmanagerdelegate (Protocol, providing delegate methods), cllocationcoodinate2d (storing coordinate locations)

In addition, cllocationmanager has several attributes;

Desiredaccuracy: location precision attribute

The values are as follows:

Kcllocationaccuracybest

Best precision

Kcllocationaccuracynearesttenmeters

Accuracy less than 10 MB

Kcllocationaccuracyhundredmeters

Accuracy less than MB

Kcllocationaccuracykilometer

Precision less than MB

Kcllocationaccuracythreekilometers

Accuracy less than MB


Distancefilter: update the location information after moving horizontally

Delegate: Response to the cllocationmanagerdelegate object


The following is an example of how to obtain a location:

First, create a single view application project, 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:

# Import "viewcontroller. H "@ interfaceviewcontroller () @ end @ implementation viewcontroller @ synthesize lonlabel; @ synthesize latlabel; @ synthesize locmanager;-(void) viewdidload {[superviewdidload]; // initialize the location manager locmanager = [[cllocationmanager alloc] init]; // sets the proxy locmanager. delegate = self; // set the location longitude locmanager. desiredaccuracy = kcllocationaccuracybest; // you can call this operation to update the location manager every 100 meters. distancefilter = 100; // start setting Bit service [locmanagerstartupdatinglocation];}-(void) viewdidunload {[selfsetlonlabel: Nil]; [selfsetlatlabel: Nil]; [superviewdidunload]; // release any retained subviews of the main view .} -(bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation {return (interfaceorientation! = Methods);}-(void) dealloc {// stop locating service [locmanagerstopupdatinglocation]; [lonlabelrelease]; [latlabelrelease]; [superdealloc];} // method in the protocol, the function is to call the delegate method-(void) locationmanager :( cllocationmanager *) manager didupdatetolocation :( cllocation *) newlocation fromlocation :( cllocation *) oldlocation {// struct whenever a location is updated, storage location coordinates cllocationcoordinate2d loc = [newlocation coordinate]; float longpolling = loc. longpolling; float latitude = loc. latitude; self. lonlabel. TEXT = [nsstringstringwithformat: @ "% F", longpolling]; self. latlabel. TEXT = [nsstringstringwithformat: @ "% F", latitude] ;}// method called when location retrieval or update fails-(void) locationmanager :( cllocationmanager *) manager didfailwitherror :( nserror *) error {nsstring * errormsg = nil; If ([error code] = kclerrordenied) {errormsg = @ "Access Denied ";} if ([error code] = kclerrorlocationunknown) {errormsg = @ "Location Information Retrieval failed";} uialertview * alertview = [[uialertviewalloc] initwithtitle: @ "location" message: errormsg delegate: Self cancelbuttontitle: @ "OK" otherbuttontitles: nil, nil]; [alertview show]; [alertview release];} @ end

Finally, compile and run the result as follows (latitude and longitude obtained on the simulator ):

To join our QQ group or public account, see: Ryan's
Zone public account and QQ Group


Welcome to my Sina Weibo chat: @ Tang Ren _ Ryan

Related Article

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.