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