CoreLocation主要应用了GPS,蜂窝基站三角网以及Wi_Fi(WPS)三种技术。
想得到定点的信息,需要涉及到几个类,CLLocationManager,CLLocation,CLLocationManagerdelegate协议,CLLocationCoodinate2D,CLLocationDegrees。
<一>先实例化一个CLLocationManager,同时设置委托及精确度等。
CCLocationManager *manager= [[CLLocationManager alloc] init]; //初始化定位器 |
[managersetDelegate: self ]; //设置代理 |
[managersetDesiredAccuracy: kCLLocationAccuracyBest]; //设置精确度 |
其中desiredAccuracy属性表示精确度,有5种选择如下:
desiredAccuracy属性 | 描述 |
kCLLocationAccuracyBest | 精确度最佳 |
kCLLocationAccuracynearestTenMeters | 精确度10m以内 |
kCLLocationAccuracyHundredMeters | 精确度100m以内 |
kCLLocationAccuracyKilometer | 精确度1000m以内 |
kCLLocationAccuracyThreeKilometers | 精确度3000m以内 |
NOTE:精确度越高,用点越多,就要根据实际情况而定。
manager.distanceFilter=250;//这个表示在地图上每隔250m才更新一次定位信息。
[managerstartUpdateLocation];启动定位器,如果不用的时候就必须调用stopUpdateLocation以关闭定位功能。
<二>CCLocation对像中包含着定点的相关信息数据。其属性主要包括coordinate,altitude,horizontalAccuracy,verticalAccuracy,timestamp等,分别如下:
coordinate用来存储地理位置的latitude和longitude,分别表示纬度和经度,都是float类型.如可这样:floatlatitude=location.coordinat.latitude;location是CCLocation的实例。这里也把上面提到的CLLocationDegrees,它其实是一个double类型,在coreLocation框架中是用来储存CLLocationCoordinate2D实例coordinate的latitude和longitude,
typedefdoubleCLLocationDegrees;
typedefstruct
{CLLocationDegreeslatitude;
CLLocationDegreeslongitude}CLLocationCoordinate2D;
altitude表示位置的海拔高度,这个值是极不准确的。
horizontalAccuracy表示水平准确度,这么理解,它是以coordinate为圆心的半径,返回的值越小,证明准确度越好,如果是负数,则表示corelocation定位失败。
verticalAccuracy表示垂直准确度,它的返回值与altitude相关,所以不准确。
Timestamp返回的是定位时的时间,是NSDate类型。
<三>CLLocationMangerDelegate协议
我们只需实现两个方法就可以了,如下:
-(void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation;
-(void)locationManager:(CLLocationManager*)manager
didFailWithError:(NSError*)error;
上面第一个是定位时候回访调,后者定位出错时被调。
<四>现在可以去实现定位了:
新建一个view-basedapplication模板的工程,假设项目名称为coreLocation.我们在contronller的头文件和源文件中的代码大概有如下:
.h
#import<UIKit/UIKit.h>
#import<CoreLocation/CoreLocation.h>
@interfaceCoreLocationViewController:UIViewController
<CLLocationManagerDelegate>{
CLLocationManager*locManager;
}
@property(nonatomic,retain)CLLocationManager*locManager;
@end
.m
#import"CoreLocationViewController.h"
@implementationCoreLocationViewController
@synthesizelocManager;
//ImplementviewDidLoadtodoadditionalsetupafterl————oadingtheview,typicallyfromanib.
-(void)viewDidLoad{
locManager=[[CLLocationManageralloc]init];
locManager.delegate=self;
locManager.desiredAccuracy=kCLLocationAccuracyBest;
[locManagerstartUpdatingLocation];
[superviewDidLoad];
}
-(void)didReceiveMemoryWarning{
//Releasestheviewifitdoesn'thaveasuperview.
[superdidReceiveMemoryWarning];
//Releaseanycacheddata,images,etcthataren'tinuse.
}
-(void)viewDidUnload{
//Releaseanyretainedsubviewsofthemainview.
//e.g.self.myOutlet=nil;
}
-(void)dealloc{
[locManagerstopUpdatingLocation];
[locManagerrelease];
[textViewrelease];
[superdealloc];
}
#pragmamark-
#pragmamarkCoreLocationDelegateMethods
-(void)locationManager:(CLLocationManager*)manager
didUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation*)oldLocation{
CLLocationCoordinate2Dlocat=[newLocationcoordinate];
floatlattitude=locat.latitude;
floatlongitude=locat.longitude;
floathorizon=newLocation.horizontalAccuracy;
floatvertical=newLocation.verticalAccuracy;
NSString*strShow=[[NSStringalloc]initWithFormat:
@"currentpos:经度=%f维度=%f水平准确读=%f垂直准确度=%f",
lattitude,longitude,horizon,vertical];
UIAlertView*show=[[UIAlertViewalloc]initWithTitle:@"coreLoacation"
message:strShowdelegate:nilcancelButtonTitle:@"igotit"
otherButtonTitles:nil];
[showshow];
[showrelease];
}
-(void)locationManager:(CLLocationManager*)manager
didFailWithError:(NSError*)error{
NSString*errorMessage;
if([errorcode]==kCLErrorDenied){
errorMessage=@"你的访问被拒绝";}
if([errorcode]==kCLErrorLocationUnknown){
errorMessage=@"无法定位到你的位置!";}
UIAlertView*alert=[[UIAlertViewalloc]
initWithTitle:nilmessage:errorMessage
delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];
[alertshow];
[alertrelease];
}
@end