怎么使用Markdowm
寫的非常詳細(xì),拿過(guò)來(lái)借用一下,有想在簡(jiǎn)書發(fā)表文章的可以看看
[簡(jiǎn)書]http://www.reibang.com/p/q81RER/
簡(jiǎn)單的定位功能,并實(shí)現(xiàn)反地理編碼
.h的聲明
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
//位置管理者
@property(nonatomic,strong)CLLocationManager * locationManager;
///緯度
@property(nonatomic,assign)CLLocationDegrees coreLati;
///經(jīng)度
@property(nonatomic,assign)CLLocationDegrees coreLong;
///編碼者
@property(nonatomic,strong)CLGeocoder * geocoder;
@end```
####.m的實(shí)現(xiàn)
-(CLGeocoder *)geocoder{
if (_geocoder==nil) {
_geocoder = [[CLGeocoder alloc]init];
}
return _geocoder;
}
- (void)viewDidLoad {
[super viewDidLoad];
// [self reverseGeoCoder];
[self startLocationService];
}
pragma mark -- 開啟定位服務(wù)
-(void)startLocationService{
//確定用戶的位置服務(wù)啟用
// if (![CLLocationManager locationServicesEnabled]
// &&[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusDenied)
// //位置服務(wù)是在設(shè)置中禁用
// {
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
//設(shè)置精確度
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
NSLog(@"使用期間定位");
[self.locationManager requestWhenInUseAuthorization];
}
//開始定位的時(shí)候不斷更新位置
[self.locationManager startUpdatingLocation];
// }
}
pragma mark -- 獲取定位的位置
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//獲取用戶的位置的對(duì)象
CLLocation * location = [locations lastObject];
//獲得當(dāng)前的坐標(biāo)
CLLocationCoordinate2D coordinate = location.coordinate;
// 獲取緯度
self.coreLati = coordinate.latitude;
// 獲取經(jīng)度
self.coreLong = coordinate.longitude;
CLLocationDegrees la = coordinate.latitude;
CLLocationDegrees lon = coordinate.longitude;
NSLog(@"定位的坐標(biāo)%lf,%lf",la,lon);
//停止更行
[self.locationManager stopUpdatingLocation];
[self reverseGeoCoder:la andlonti:lon];
}
pragma mark -- 定位后反地理編碼
-(void)reverseGeoCoder:(double)lati andlonti:(double)longti{
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:lati longitude:longti];
// 反地理編碼(經(jīng)緯度---地址)
[self.geocoder reverseGeocodeLocation:location1 completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if(error == nil)
{
CLPlacemark *pl = [placemarks firstObject];
//獲得的定位信息
NSString * str = pl.name;
//獲得緯度
NSString * str1 = @(pl.location.coordinate.latitude).stringValue;
//獲得經(jīng)度
NSString * str2 = @(pl.location.coordinate.longitude).stringValue;
//獲得所在的位置(某市)
NSString * str3 = placemarks.firstObject.locality;
//獲得所在市的某區(qū)
NSString * str4 = placemarks.firstObject.subLocality;
//獲得省份(形成區(qū)域)
NSString * str5 = placemarks.firstObject.administrativeArea;
NSLog(@"str -> %@,str1 -> %@,str2 -> %@,str3 -> %@,str4 -> %@,str5->%@",str,str1,str2,str3,str4,str5);
}else
{
NSLog(@"錯(cuò)誤");
}
}];
} ```