Core location mainly uses GPS, cellular base station triangular network and WI_FI (WPS) three kinds of technology.
- Using the GPS positioning system, you can pinpoint your current location, but because GPS receivers need to be aligned to the sky to work, it is basically useless in the indoor environment.
- Another effective way to find your location is to use your mobile phone base station, which will keep in touch with the surrounding base station, and if you know the identity of these base stations, you can use a variety of databases ( Include the identity of the base station and their exact geographic location) to calculate the physical position of the phone. The base station does not require satellites, and unlike GPS, it works as well for indoor environments. But it is not as accurate as GPS, its accuracy depends on the density of the base station, it is the most accurate in the base station-intensive area.
- The third way is to rely on Wi-Fi, when using this method, the device connects to a Wi-Fi network , by checking the service provider's data to determine the location, it does not rely on satellites, nor rely on the base station, so this method for can be connected to The area of the Wi-Fi network is valid, but its accuracy is also the worst of the three methods.
To get a fixed point of information, need to involve 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, there are 5 options:
desiredaccuracy Properties |
description |
kcllocationaccuracybest |
accuracy best |
kcllocationaccuracynearesttenmeters |
Accuracy within 10m |
Kcllocationaccuracyhundredmeters |
Accuracy within 100m |
kcllocationaccuracykilometer |
accuracy within 1000m |
kcllocationaccuracythreekilometers |
Within 3000m of accuracy |
Note: The higher the accuracy, the more points you will have to make according to the actual situation.
Manager.distancefilter =+//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 latitude and longitude, which are used to store geographic locations, represent latitude and longitude, respectively, and are float types. If so: 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 just 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 positioning time to return the adjustment, the latter position when the error is adjusted.
< four > can now be used to achieve positioning:
Create a new project for the view-based application template, assuming the project name is corelocation. The code in our Contronller 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 does additional setup after loading the view, typically from a nib.
-(void) Viewdidload {
Locmanager = [[Cllocationmanager alloc] init];
Locmanager.delegate = 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-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 coordinate];
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 cancelbuttontitle:@ "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 cancelbuttontitle:@ "OK" Otherbuttontitles:nil];
[Alert show];
[Alert release];
}
@end
Core Location Locator