1. IOS定位服務的開啟與基本設置
1. 要想使用IOS中的定位服務首先需要包含頭文件CoreLocation/CoreLocation.h,在interface中聲明一個屬性locationManager
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
// 定位管理器
@property (strong, nonatomic) CLLocationManager * locationManager;
@end
2. 懶加載
@implementation ViewController
- (CLLocationManager *)locationManager {
if (!_locationManager) {
// 實例化管理器
_locationManager = [[CLLocationManager alloc] init];
// 設置管理器類型為普通
_locationManager.activityType = CLActivityTypeOther;
// 設置精度為最高
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// 設置自動過濾值
_locationManager.distanceFilter = 10;
// 設置代理
// 協議中的方法和定位結果有關
_locationManager.delegate = self;
}
return _locationManager;
}
@end
2. 檢測應用程序的授權狀態
// 獲取定位服務授權狀態
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
// 判斷是否已經授權
if (status == kCLAuthorizationStatusNotDetermined) {
NSLog(@"未授權狀態");
// 當使用應用程序時使用定位服務
[self.locationManager requestWhenInUseAuthorization];
} else if (status == kCLAuthorizationStatusDenied) {
NSLog(@"授權被拒絕");
// 創建一個模態警告視圖控制器
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"親,你還沒有開啟定位服務,現在設置好嗎?" preferredStyle:UIAlertControllerStyleActionSheet];
// 打開設置的按鈕
UIAlertAction *openAction = [UIAlertAction actionWithTitle:@"打開設置" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// 點擊設置按鈕的回調方法
// 打開應用程序的設置
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
// 通過UIApplication對象的openURL方法可以
// 打電話 - tel://號碼
// 發短信 - sms://號碼
// 打開網頁 - http(s)://網址
// 打開App Store - itms-apps://應用地址
// NSURL *url2 = [NSURL URLWithString:@"tel://1008611"];
// [[UIApplication sharedApplication] openURL:url2];]
[[UIApplication sharedApplication] openURL:url];
}];
// 取消按鈕
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// 將兩個UIAlertAction(相當于兩顆按鈕)加到視圖控制器上
[alertController addAction:openAction];
[alertController addAction:cancelAction];
// 以模態的方式顯示警告視圖控制器
[self presentViewController:alertController animated:YES completion:nil];
}
3. 開始定位
// 開始定位
[self.locationManager startUpdatingLocation];
// 通過協議方法獲取定位信息
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
// locations是獲取的位置信息集合
NSLog(@"%@", locations.firstObject);
}
4. 范圍監測
#pragma mark - 監測區域
- (void) monitorRange {
// 監測中心
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(30.000000, 104.000000);
// 創建監測范圍對象
// 參數1:監測中心
// 參數2:監測半徑
// 參數3:標識
CLCircularRegion * region = [[CLCircularRegion alloc] initWithCenter:center radius:200 identifier:@"home"];
// 開始檢測目標范圍
[self.locationManager startMonitoringForRegion:region];
}
#pragma mark - CLLocationManagerDelegate
// 進入監測范圍
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"你已進入監測區域");
}
// 離開監測范圍
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(@"你已離開了監測區域");
}
5. 地理編碼和反編碼
#import "ZFGeocoder.h"
#import <CoreLocation/CoreLocation.h>
@implementation ZFGeocoder
// 通過類方法創建一個地理編碼管理器
+ (instancetype) createGeocoder {
ZFGeocoder * geocoder = nil;
if (!geocoder) {
geocoder = [[ZFGeocoder alloc] init];
}
return geocoder;
}
// 將地址編碼編成對應的經緯度
+ (void)getCoordinateWithAddress:(NSString *)address didFinished:(void (^)(CLLocationCoordinate2D))coordinate {
//將地址編碼成對應的經緯度
[[self createGeocoder] geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//返回值
CLLocationCoordinate2D tcoordinate = CLLocationCoordinate2DMake(0, 0);
for (CLPlacemark * mark in placemarks) {
//拿到經緯度
tcoordinate = mark.location.coordinate;
}
//調用block傳值
coordinate(tcoordinate);
}];
}
// 將經緯度進行反編碼
+ (void)getAddressWithCoordinate:(CLLocationCoordinate2D)coordinate didFinished:(void (^)(NSDictionary *))address {
//拿到解析器
CLGeocoder * geo = [self createGeocoder];
//反編碼
CLLocation * location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[geo reverseGeocodeLocation:location completionHandler:^(NSArray * placemarks, NSError * error) {
NSDictionary * dict;
//參數1:反編碼后的結果
for (CLPlacemark * mark in placemarks) {
//拿到返回值
dict = mark.addressDictionary;
// NSLog(@"%@", mark.addressDictionary);
// //獲取詳細信息
// NSLog(@"%@", mark.addressDictionary[@"FormattedAddressLines"][0]);
// //國家
// NSLog(@"國家:%@",mark.addressDictionary[@"Country"]);
//
// //街道:
// NSLog(@"街道:%@", mark.addressDictionary[@"Street"]);
}
//調用block傳值
address(dict);
}];
}
@end
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。