怎么使用Markdowm
写的非常详细,拿过来借用一下,有想在简书发表文章的可以看看
[简书]http://www.jianshu.com/p/q81RER/
简单的定位功能,并实现反地理编码
.h的声明
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
//位置管理者
@property(nonatomic,strong)CLLocationManager * locationManager;
///纬度
@property(nonatomic,assign)CLLocationDegrees coreLati;
///经度
@property(nonatomic,assign)CLLocationDegrees coreLong;
///编码者
@property(nonatomic,strong)CLGeocoder * geocoder;
@end```
####.m的实现
-(CLGeocoder *)geocoder{
if (_geocoder==nil) {
_geocoder = [[CLGeocoder alloc]init];
}
return _geocoder;
}
- (void)viewDidLoad {
[super viewDidLoad];
// [self reverseGeoCoder];
[self startLocationService];
}
pragma mark -- 开启定位服务
-(void)startLocationService{
//确定用户的位置服务启用
// if (![CLLocationManager locationServicesEnabled]
// &&[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusDenied)
// //位置服务是在设置中禁用
// {
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
//设置精确度
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
NSLog(@"使用期间定位");
[self.locationManager requestWhenInUseAuthorization];
}
//开始定位的时候不断更新位置
[self.locationManager startUpdatingLocation];
// }
}
pragma mark -- 获取定位的位置
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//获取用户的位置的对象
CLLocation * location = [locations lastObject];
//获得当前的坐标
CLLocationCoordinate2D coordinate = location.coordinate;
// 获取纬度
self.coreLati = coordinate.latitude;
// 获取经度
self.coreLong = coordinate.longitude;
CLLocationDegrees la = coordinate.latitude;
CLLocationDegrees lon = coordinate.longitude;
NSLog(@"定位的坐标%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];
// 反地理编码(经纬度---地址)
[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;
//获得经度
NSString * str2 = @(pl.location.coordinate.longitude).stringValue;
//获得所在的位置(某市)
NSString * str3 = placemarks.firstObject.locality;
//获得所在市的某区
NSString * str4 = placemarks.firstObject.subLocality;
//获得省份(形成区域)
NSString * str5 = placemarks.firstObject.administrativeArea;
NSLog(@"str -> %@,str1 -> %@,str2 -> %@,str3 -> %@,str4 -> %@,str5->%@",str,str1,str2,str3,str4,str5);
}else
{
NSLog(@"错误");
}
}];
} ```