【iOS開發-109】CoreLocation架構的兩個主要類CLLocationManager和CLGeoCoder介紹

來源:互聯網
上載者:User

標籤:

CoreLocation架構主要由兩個常用的類,一個是CLLocationManager,一個是CLGeoCoder。


(1)CoreLocation的使用,先匯入CoreLocation架構。


(2)一般是利用位置管理器來操作,即CLLocationManager

——開啟,就是startUpdatingLocation;關閉,就是stopUpdatingLocation

——可以先判斷位置服務是否開啟locationServicesEnabled,如果沒開啟,直接返回空,不操作。

——iOS8的推送資訊,需要獲得使用者允許,註冊以下推播通知

——位置類CLLocation有很多屬性,包括方向course,還有座標coordinate,coordinate有經緯度屬性latitude和longitude。

#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController ()<CLLocationManagerDelegate>@property(nonatomic,strong) CLLocationManager *locMgr;@end@implementation ViewController-(CLLocationManager *)locMgr{    //判斷,定位服務是否開啟,如果沒開啟,直接返回nil,不執行下面的操作    if (![CLLocationManager locationServicesEnabled]) return nil;        if (!_locMgr) {        _locMgr=[[CLLocationManager alloc]init];    }    return _locMgr;}- (void)viewDidLoad {    [super viewDidLoad];    if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {                    }    //設定代理    self.locMgr.delegate=self;    [self.locMgr startUpdatingLocation];}-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{    CLLocation *loc=[locations firstObject];    NSLog(@"%f %f",loc.coordinate.latitude,loc.coordinate.longitude);    //如果只需要調用1次,則立即關閉,因為預設每隔一分鐘調用一次這個方法,耗電    [manager stopUpdatingLocation];}@end

——推播通知允許的個人化



(3)計算兩個位置之間的直線距離

    CLLocation *loc1=[[CLLocation alloc]initWithLatitude:44 longitude:120];    CLLocation *loc2=[[CLLocation alloc]initWithLatitude:45 longitude:120];    CLLocationDistance distance=[loc1 distanceFromLocation:loc2];    NSLog(@"%f",distance);

結果是:111122.007528

表示位置的屬性還有海拔等等,用如下方法可以一窺:

CLLocation *loc2=[[CLLocation alloc]initWithCoordinate:(CLLocationCoordinate2D) altitude:(CLLocationDistance) horizontalAccuracy:(CLLocationAccuracy) verticalAccuracy:(CLLocationAccuracy) timestamp:(NSDate *)];

當然,我們可以自己make一個座標等等。


(4)還有一些針對位置管理器的屬性介紹

    //每隔多遠距離更新一次    self.locMgr.distanceFilter=distance;    //精度設定    self.locMgr.desiredAccuracy=kCLLocationAccuracyBestForNavigation;

(5)在iOS7中,需要使用者授權,但是不需要開發人員額外設定,會自動有彈窗出來。但是在iOS8中,儘管註冊了推送訊息的通知,但是仍然無效,需要有特殊設定:

——首先,最核心的就是調用方法requestWhenInUseAuthorization或者requestAlwaysAuthorization,兩者區別在於,一個是告訴使用者只在使用者使用這個app時訪問位置資訊,而後者是不管你正在用或者放在後台,都使用位置資訊。而且區別在於,如果選擇後者的話,那麼彈框出來的資訊是帶有自訂的資訊的(即上面所說的個人化說明資訊)。

- (void)viewDidLoad {    [super viewDidLoad];    if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {        [self.locMgr requestWhenInUseAuthorization];    }    [self.locMgr startUpdatingLocation];}

——當然,僅僅調用以上語句還是不夠。還需要在info.plist中添加對應的屬性,當然,兩個都添加了也沒事。



(6)如果不需要關心使用者的地點資訊,只需要簡單地經緯度和地址之間的轉換,那麼就是地理編碼和發地理編碼。

地理編碼:把地點名稱,轉換成經緯度以及更詳細的地址。反地理編碼,就是輸入經緯度,擷取詳細地址。主要代碼如下:

- (IBAction)geoCode:(id)sender {    if (self.addressField.text.length==0) return;    [self.geocoder geocodeAddressString:self.addressField.text completionHandler:^(NSArray *placemarks, NSError *error) {        if (error) {            [email protected]"找不到你輸入的詳細地址";        }else{            CLPlacemark *pm=[placemarks firstObject];            self.longitudeLabel.text=[NSString stringWithFormat:@"%.1f",pm.location.coordinate.longitude];            self.latitudeLabel.text=[NSString stringWithFormat:@"%.1f",pm.location.coordinate.latitude];            self.detailAddressLabel.text=pm.name;        }    }];}- (IBAction)reverseGeoCode:(id)sender {    if (self.reverseLongitudeField.text.length==0 || self.reverseLatitudeField.text.length==0) return;    CLLocation *loc=[[CLLocation alloc]initWithLatitude:[self.reverseLatitudeField.text doubleValue] longitude:[self.reverseLongitudeField.text doubleValue]];    [self.geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {        if (error) {            [email protected]"這是什麼經緯度?";        }else{            CLPlacemark *pm=[placemarks firstObject];            self.reverseDetailAddressLabel.text=pm.name;        }    }];}



(7)補充CLLocationManager的另一個知識,範圍的監控。比如360兒童手錶的進入某個地區和離開某個地區就警報的功能。

——得遵守協議<CLLocationManagerDelegate>

——然後,建立一個地區,下面的第一個方法已經報銷了,現在用第二個方法,即CLRegion用CLCircularRegion來建立這個不太容易想到。

CLRegion *region=[[CLRegion alloc]initCircularRegionWithCenter:(CLLocationCoordinate2D) radius:(CLLocationDistance) identifier:@"china"];CLRegion *region=[[CLCircularRegion alloc]initWithCenter:(CLLocationCoordinate2D) radius:(CLLocationDistance) identifier:(NSString *)];
——然後,發起監控

[self.locMgr startMonitoringForRegion:region];

——當然,有代理方法,即進入這個範圍或者離開的時候,做什麼事情,就在代理方法裡做:

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{    }-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{    }

——當然,關閉監控地區的方法是:

[self.locMgr stopMonitoringForRegion:region];

【iOS開發-109】CoreLocation架構的兩個主要類CLLocationManager和CLGeoCoder介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.