一、CLLocation的使用
CLLocation用來表示某個位置的地理信息,比如經(jīng)緯度、海拔等等。
// 經(jīng)緯度
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
// 海拔
@property(readonly, nonatomic) CLLocationDistance altitude;
// 路線,航向(取值范圍是0.0° ~ 359.9°,0.0°代表真北方向)
@property(readonly, nonatomic) CLLocationDirection course;
// 行走速度(單位是m/s)
@property(readonly, nonatomic) CLLocationSpeed speed;
// 定位信息返回的時間
@property(readonly, nonatomic, copy) NSDate *timestamp;
// 水平精準度
@property(readonly, nonatomic) CLLocationAccuracy horizontalAccuracy;
// 垂直精準度
@property(readonly, nonatomic) CLLocationAccuracy verticalAccuracy;
// 可以計算2個位置之間的距離
- (CLLocationDistance)distanceFromLocation:(const CLLocation *) location
// 計算兩次的距離(單位時米)
CLLocationDistance distance = [newLocation distanceFromLocation:self.previousLocation];
// 計算兩次之間的時間(單位只秒)
NSTimeInterval dTime= [newLocation.timestamp timeIntervalSinceDate:self.previousLocation.timestamp];
注:CLLocationCoordinate2D是一個用來表示經(jīng)緯度的結(jié)構(gòu)體,定義如下:
typedef struct {
CLLocationDegrees latitude; // 緯度
CLLocationDegrees longitude; // 經(jīng)度
} CLLocationCoordinate2D;
一般用CLLocationCoordinate2DMake函數(shù)來創(chuàng)建CLLocationCoordinate2D。
二、區(qū)域檢測
- 開始檢測用戶所在的區(qū)域
// 1、創(chuàng)建中心點
CLLocationCoordinate2Dcenter = CLLocationCoordinate2DMake(45.058501, 120.304171);
//2、創(chuàng)建圓形區(qū)域, 指定區(qū)域中心點的經(jīng)緯度,以及半徑
CLCircularRegion*circular = [[CLCircularRegion alloc] initWithCenter:center radius:500 identifier:@"中關(guān)村軟件園"];
[self.manager startMonitoringForRegion:circular];
注意:CLRegion 有兩個子類是專門用于指定區(qū)域的:
- CLBeaconRegion:可以指定藍牙的范圍
- CLCircularRegion:可以指定圓形的范圍
2.實現(xiàn)CLLocationManager 的代理方法
// 進入監(jiān)聽區(qū)域時調(diào)用
- (void)locationManager:(CLLocationManager *)manager didEnterRegion: (CLRegion *)region {
NSLog(@"進入監(jiān)聽區(qū)域時調(diào)用");
}
// 離開監(jiān)聽區(qū)域時調(diào)用
- (void)locationManager:(CLLocationManager *)manager didExitRegion: (CLRegion *)region {
NSLog(@"離開監(jiān)聽區(qū)域時調(diào)用");
}
```
#三、CLGeocoder(地理編碼和反地理編碼)
1.地理編碼:根據(jù)給定的地名,獲得具體的位置信息(比如經(jīng)緯度、地址的全稱等)。
地理編碼的實現(xiàn):
- 導入#import <CoreLocation/CoreLocation.h>
- 懶加載CLGeocoder地理編碼對象。
- 獲取用戶的位置
- 利用地理編碼對象,根據(jù)傳入的地址獲取該地址對應的經(jīng)緯度信息。
[self.geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks.count == 0 || error != nil) {
return ;
}
for (CLPlacemark *placemark in placemarks) {
NSLog(@"%@ %@ %f %f",placemark.name,placemark.addressDictionary,
placemark.location.coordinate.latitude,placemark.location.coordinate.longitude);
}
}];
2.反地理編碼:根據(jù)給定的經(jīng)緯度,獲得具體的位置信息。
反地理編碼的實現(xiàn):
- 導入#import <CoreLocation/CoreLocation.h>
- 懶加載CLGeocoder地理編碼對象。
- 獲取用戶的經(jīng)緯度
- 根據(jù)經(jīng)緯度創(chuàng)建CLLocation對象
- 利用地理編碼對象,根據(jù)傳入的經(jīng)緯度獲取該地址對應的地標信息。
CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longtitude doubleValue]];
[self.geocoder reverseGeocodeLocation:location
completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *placemark in placemarks) {
NSLog(@"%@ %@ %f %f",placemark.name,placemark.addressDictionary,placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
}
}];
#四、CLPlacemark
CLPlacemark的字面意思是地標,封裝詳細的地址位置信息。
@property (nonatomic, readonly) CLLocation *location; //地理位置
@property (nonatomic, readonly) CLRegion *region; //區(qū)域
@property (nonatomic, readonly) NSDictionary *addressDictionary; //詳細的地址信息
@property (nonatomic, readonly) NSString *name; //地址名稱
@property (nonatomic, readonly) NSString *locality; // 位置