IOS Core location for positioning

Source: Internet
Author: User

In the iphone, you can use the core location feature to achieve geolocation, and use the Mapkit framework to load Google Maps. To get a fixed point of information, in fact, it is not difficult, only need to relate to several classes, Cllocationmanager, Cllocation, cllocationmanagerdelegate protocol, CLLOCATIONCOODINATE2D, Cllocationdegrees. < a > Instantiate a Cllocationmanager, and set the delegate and accuracy. Cclocationmanager *manager = [[Cllocationmanager alloc] init]; [Manager Setdelegate:self];              [Manager Setdesiredaccuracy:kcllocationaccuracybest]; where the desiredaccuracy attribute represents accuracy, the 5 options are as follows: Desiredaccuracy property Description Kcllocationaccuracybest accuracy Best kcllocationaccuracynearesttenmeters accuracy 10m within kcllocationaccuracyhundredmeters accuracy within 100m KCL Locationaccuracykilometer accuracy within 1000m kcllocationaccuracythreekilometers accuracy within 3000m Note: The higher the accuracy, the more points to use, it depends on the actual situation. Manager.distancefilter = 250; This means that the location information is updated every 250m on the map. [Manager Startupdatelocation]; Start the locator, and if not, you must call Stopupdatelocation to close the positioning function. < Two >cclocation contains the relevant information data about the fixed point in the image. Its properties mainly include coordinate, altitude,horizontalaccuracy,verticalaccuracy, timestamp, etc., respectively, as follows: coordinate The latitude and longitude, which are used to store the geographic location, represent both latitude and longitude, and are all float types. If this is the case: float latitude = locatIon.coordinat.latitude; Location is an example of cclocation. Here also the above mentioned cllocationdegrees, it is actually a double type, in the core location frame is used to store cllocationcoordinate2d instance coordinate latitude   and Longitude,typedef double cllocationdegrees;typedef struct {cllocationdegrees latitude; Cllocationdegrees Longitude} Cllocationcoordinate2d;altitude represents the altitude of the position, which is highly inaccurate. Horizontalaccuracy represents the level of accuracy, so to understand, it is the radius of the center of the coordinate, the smaller the return value, the better the proof of accuracy, if it is negative, it means that the core location failed. Verticalaccuracy represents the vertical accuracy, and its return value is related to altitude, so it is inaccurate. Timestamp returns the time at which it was positioned, and is the NSDate type. < three >cllocationmangerdelegate protocol we only need to implement two methods, as follows:-(void) Locationmanager: (Cllocationmanager *) Manager Didupdatetolocation: (cllocation *) newlocation fromlocation: (cllocation *) oldlocation;-(void) Locationmanager: ( Cllocationmanager *) Manager didfailwitherror: (nserror *) error, the first of the above is the position when the return adjustment, the latter position when the error is adjusted. < four > Now it's time to do the positioning: Create a new view-based application template, assuming the project name is corelocation. Our code in Contronller's header and source files is probably as follows:. h# Import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interFace Corelocationviewcontroller:uiviewcontroller <cllocationmanagerdelegate>{CLLocationManager *locManager;} @property (nonatomic, retain) Cllocationmanager *locmanager; @end. M#import "CoreLocationViewController.h" @ Implementation Corelocationviewcontroller@synthesize locmanager;//Implement Viewdidload to doing additional setup after Loading the view, typically from a nib.-(void) viewdidload {locmanager = [[Cllocationmanager alloc] Init];locmanager.deleg ate = Self;locmanager.desiredaccuracy = Kcllocationaccuracybest;    [Locmanager startupdatinglocation]; [Super Viewdidload];}    -(void) didreceivememorywarning {//Releases the view if it doesn ' t has a superview. [Super didreceivememorywarning];//Release Any cached data, images, etc this aren ' t in use.} -(void) Viewdidunload {//Release any retained subviews of the main view.//e.g. Self.myoutlet = nil;} -(void) Dealloc {[Locmanager stopupdatinglocation];[ Locmanager release];    [TextView release]; [Super Dealloc];} #pragma mark-#pragMa Mark corelocation Delegate methods-(void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: ( Cllocation *) newlocation fromlocation: (cllocation *) oldlocation {cllocationcoordinate2d locat = [NewLocation coordinat E];float Lattitude = locat.latitude;float Longitude = locat.longitude;float Horizon = newlocation.horizontalaccuracy; float vertical = newlocation.verticalaccuracy; NSString *strshow = [[NSString alloc] initwithformat:@ "Currentpos: Longitude =%f dimension =%f level accurate read =%f vertical accuracy =%f", lattitude, longitude, Horizon, Vertical]; Uialertview *show = [[Uialertview alloc] initwithtitle:@ "coreloacation" Message:strshow delegate:nil CancelButt ontitle:@ "I got It" otherbuttontitles:nil]; [Show show]; [Show release];} -(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error{nsstring *errormessage;if ([ Error code] = = kclerrordenied) {errormessage = @ "Your access is denied";} if ([error code] = = Kclerrorlocationunknown) {errormesSage = @ "Cannot navigate to your location!";} Uialertview *alert = [[Uialertview alloc] Initwithtitle:nil message:errormessage delegate:self CancelButton title:@ "determine" otherbuttontitles:nil]; [Alert show]; [Alert release];} @end

IOS Core location for positioning

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.