IOS開發之Core Location


IOS 支持三種檢測當前位置的方式:手機基站、Wi-Fi、和GPS,其中GPS是經度最高的,同時也是最耗費手機電量的。一般情況下在室內是無法通過GPS獲 取位置信息的,通過Wi-Fi獲取位置的原理是通過網絡提供商的IP地址信息來獲取位置,經度不是很高,最后是通過手機基站獲取位置,手機開機后會連接附 近的基站塔獲取信號,通過基站可以得到手機所在的位置信息,基站越密集,所獲取的位置信息經度就越高。


IOS SDK提供的Core Location能比較好的提供獲取位置信息的功能,獲取位置信息涉及如下幾個類,CLLocationManager(位置管理器), CLLocation, CLLocationManagerdelegate(協議、提供委托方法),CLLocationCoodinate2D(存儲坐標位置)

另外CLLocationManager還有幾個屬性;

desiredAccuracy:位置的精度屬性

取值有如下幾種:

kCLLocationAccuracyBest

精確度最佳

kCLLocationAccuracynearestTenMeters

精確度10m以內

kCLLocationAccuracyHundredMeters

精確度100m以內

kCLLocationAccuracyKilometer

精確度1000m以內

kCLLocationAccuracyThreeKilometers

精確度3000m以內


distanceFilter:橫向移動多少距離后更新位置信息

delegate:響應CLLocationManagerdelegate的對象


下面來構建一個獲取位置的例子:

首先建立一個Single View Application工程,然后引入CoreLocation.framework,並在ViewController.h中修改如下:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager *locManager;
}

@property (retain, nonatomic) IBOutletUILabel *lonLabel;
@property (retain, nonatomic) IBOutletUILabel *latLabel;
@property (retain, nonatomic) CLLocationManager *locManager;

@end
.m文件的實現如下,具體解釋在代碼注釋中說明

#import  " ViewController.h "

@interfaceViewController ()

@end

@implementation ViewController
@synthesize lonLabel;
@synthesize latLabel;
@synthesize locManager;

- ( void)viewDidLoad
{
    [superviewDidLoad];
    
     // 初始化位置管理器
    locManager = [[CLLocationManager alloc]init];
     // 設置代理
    locManager. delegate = self;
     // 設置位置經度
    locManager.desiredAccuracy = kCLLocationAccuracyBest;
     // 設置每隔100米更新位置
    locManager.distanceFilter =  100;
     // 開始定位服務
    [locManagerstartUpdatingLocation];
}

- ( void)viewDidUnload
{
    [selfsetLonLabel:nil];
    [selfsetLatLabel:nil];
    [superviewDidUnload];
     //  Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- ( void)dealloc {
     // 停止定位服務
    [locManagerstopUpdatingLocation];
    [lonLabelrelease];
    [latLabelrelease];
    [superdealloc];
}

// 協議中的方法,作用是每當位置發生更新時會調用的委托方法
-( void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
     // 結構體,存儲位置坐標
    CLLocationCoordinate2D loc = [newLocation coordinate];
     float longitude = loc.longitude;
     float latitude = loc.latitude;
    self.lonLabel.text = [NSStringstringWithFormat: @" %f ",longitude];
    self.latLabel.text = [NSStringstringWithFormat: @" %f ",latitude];
    
}

// 當位置獲取或更新失敗會調用的方法
-( void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSString *errorMsg = nil;
     if ([error code] == kCLErrorDenied) {
        errorMsg =  @" 訪問被拒絕 ";
    }
     if ([error code] == kCLErrorLocationUnknown) {
        errorMsg =  @" 獲取位置信息失敗 ";
    }
    
    UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle: @" Location " 
                                                   message:errorMsg  delegate:self cancelButtonTitle: @" Ok "otherButtonTitles:nil, nil];
    [alertView show];
    [alertView release];
}

@end
最后編譯並運行結果如下(在模擬器上得到的經緯度):


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM