Core Location主要應用了GPS, 蜂窩基站三角網以及Wi_Fi(WPS)三種技術。html
想獲得定點的信息,須要涉及到幾個類,CLLocationManager, CLLocation, CLLocationManagerdelegate協議,CLLocationCoodinate2D, CLLocationDegrees。git
<一>先實例化一個CLLocationManager,同時設置委託及精確度等。數據庫
CCLocationManager *manager = [[CLLocationManager alloc] init]; //初始化定位器 |
[manager setDelegate: self ]; //設置代理 |
[manager setDesiredAccuracy: kCLLocationAccuracyBest]; //設置精確度 |
其中desiredAccuracy屬性表示精確度,有5種選擇以下:網絡
desiredAccuracy屬性app |
描述框架 |
kCLLocationAccuracyBestatom |
精確度最佳spa |
kCLLocationAccuracynearestTenMeters代理 |
精確度10m之內rest |
kCLLocationAccuracyHundredMeters |
精確度100m之內 |
kCLLocationAccuracyKilometer |
精確度1000m之內 |
kCLLocationAccuracyThreeKilometers |
精確度3000m之內 |
NOTE:精確度越高,用點越多,就要根據實際狀況而定。
manager.distanceFilter = 250;//這個表示在地圖上每隔250m才更新一次定位信息。
[manager startUpdateLocation]; 啓動定位器,若是不用的時候就必須調用stopUpdateLocation以關閉定位功能。
<二>CCLocation對像中包含着定點的相關信息數據。其屬性主要包括coordinate, altitude,horizontalAccuracy,verticalAccuracy, timestamp等,分別以下:
coordinate 用 來存儲地理位置的latitude和longitude,分別表示緯度和經度,都是float類型.如可這 樣: float latitude = location.coordinat.latitude; location是CCLocation的實例。 這裏也把上面提到的CLLocationDegrees,它實際上是一個double類型,在core Location框架中是用來儲存 CLLocationCoordinate2D實例coordinate的latitude 和longitude,
typedef double CLLocationDegrees;
typedef struct
{CLLocationDegrees latitude;
CLLocationDegrees longitude} CLLocationCoordinate2D;
altitude 表示位置的海拔高度,這個值是極不許確的。
horizontalAccuracy 表示水平準確度,這麼理解,它是以coordinate爲圓心的半徑,返回的值越小,證實準確度越好,若是是負數,則表示core location定位失敗。
verticalAccuracy表示垂直準確度,它的返回值與altitude相關,因此不許確。
Timestamp 返回的是定位時的時間,是NSDate類型。
<三>CLLocationMangerDelegate協議
咱們只需實現兩個方法就能夠了,以下:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation ;
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error;
上面第一個是定位時候回訪調,後者定位出錯時被調。
<四>如今能夠去實現定位了:
新建一個view-based application模板的工程,假設項目名稱爲coreLocation.咱們在contronller的頭文件和源文件中的代碼大概有以下:
.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 do 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 have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that 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: 經度=%f 維度=%f 水平準確讀=%f 垂直準確度=%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 = @"你的訪問被拒絕";}
if ([error code] == kCLErrorLocationUnknown) {
errorMessage = @"沒法定位到你的位置!";}
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:nil message:errorMessage
delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil];
[alert show];
[alert release];
}
@end