iPhone開發(fā)應(yīng)用中CoreLocation定位學(xué)習(xí)筆記
作者:佚名
本文介紹的是iPhone開發(fā)應(yīng)用中CoreLocation定位的內(nèi)容,iPhone可以使用CoreLocation框架確定他的物理位置,可以利用三種技術(shù)來實(shí)現(xiàn)該功能:GPS,WiFi定位和蜂窩基站三角網(wǎng)定位。
iPhone開發(fā)應(yīng)用中CoreLocation定位學(xué)習(xí)是本文要介紹的內(nèi)容,iPhone可以使用CoreLocation框架確定他的物理位置,可以利用三種技術(shù)來實(shí)現(xiàn)該功能:GPS,WiFi定位和蜂窩基站三角網(wǎng)定位。但在程序中我們只需設(shè)定我們希望的精度級別,由CoreLocation決定采用哪種技術(shù)可以更好的滿足我們的請求。
1、位置管理器
- CLLocationManager *locationManager = [[CLLocationManager alloc] init];//創(chuàng)建位置管理器
- locationManager.delegate=self;//設(shè)置代理
- locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度級別
- locationManager.distanceFilter=1000.0f;//設(shè)置距離篩選器
- [locationManager startUpdatingLocation];//啟動位置管理器
2、位置管理器代理
主要的代理方法有兩個
- //確定當(dāng)前位置和位置更新了時調(diào)用這個方法
- - (void)locationManager:(CLLocationManager *)manager
- didUpdateToLocation:(CLLocation *)newLocation
- fromLocation:(CLLocation *)oldLocation
- {
- NSString *latitudeString=[[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.latitude];
- //latitudeLabel.text=latitudeString;
- [latitudeString release];
- NSString *longitudeString=[[NSString alloc] initWithFormat:@"%g",newLocation.coordinate.longitude];
- //longitudeLabel.text=longitudeString;
- [longitudeString release];
- }
- //位置查詢遇到錯誤時調(diào)用這個方法
- (void)locationManager:(CLLocationManager *)manager
- didFailWithError:(NSError *)error
- {
- NSString *errorType = (error.code == kCLErrorDenied) ?
- @"Access Denied" : @"Unknown Error";
- UIAlertView *alert = [[UIAlertView alloc]
- initWithTitle:@"Error getting Location"
- message:errorType
- delegate:nil
- cancelButtonTitle:@"Okay"
- otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
小結(jié):iPhone開發(fā)應(yīng)用中CoreLocation定位學(xué)習(xí)筆記的內(nèi)容介紹完了,希望本文對你有所幫助。
責(zé)任編輯:zhaolei
來源:
互聯(lián)網(wǎng)