iOS Core Location 實現定位

在iphone中可以用core location功能來實現地理定位,並可用mapkit 框架加載google地圖。
想得到定點的信息,其實 不難,只需要涉及到幾個類,CLLocationManager, CLLocation, CLLocationManagerdelegate協 議,CLLocationCoodinate2D, CLLocationDegrees。
<一>先實例化一個CLLocationManager,同時設置委託及精確度等。
CCLocationManager *manager = [[CLLocationManager alloc] init];
[manager setDelegate: self];
[manager setDesiredAccuracy: kCLLocationAccuracyBest];
其中desiredAccuracy屬性表示精確度,有利5種選擇如下:
       desiredAccuracy屬性
              描述
kCLLocationAccuracyBest
精確度最佳
kCLLocationAccuracynearestTenMeters
精確度10m以內
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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章