地图的基础使用

导入第三方类库

#import <CoreLocation/CoreLocation.h>

声明属性

@interface ViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLGeocoder *geoCoder;

@end

在ViewDidLoad中写属性和方法

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化
    self.locationManager = [[CLLocationManager alloc] init];
    // 设置代理
    self.locationManager.delegate = self;
    // 定位精度
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 设置多少米更新一次距离
    self.locationManager.distanceFilter = 100;
    // 什么时候请求位置信息
    [self.locationManager requestAlwaysAuthorization];
    // 开始请求位置信息
    [self.locationManager startUpdatingLocation];


    // 将位置信息转换为经纬度
    [self.geoCoder geocodeAddressString:@"信阳" completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *pm = [placemarks firstObject];
        // 经度
        CGFloat longitude = pm.location.coordinate.longitude;
        // 纬度
        CGFloat latitude = pm.location.coordinate.latitude;
        NSLog(@"经度:%f, 纬度:%f", longitude, latitude);
    }];



}

代理方法

// 定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // 将地理位置转换为汉字
    CLLocation *location = [locations firstObject];
    [self.geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
        } else {
            CLPlacemark *pm = [placemarks firstObject];
            NSLog(@"%@", pm.addressDictionary);
        }
    }];
//    NSLog(@"%@", location);
//    NSLog(@"纬度:%f", location.coordinate.latitude);
//    NSLog(@"经度%f", location.coordinate.longitude);

}

// 定位失败
-  (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    // 错误信息描述
    NSLog(@"%@", [error localizedDescription]);
}


#pragma mark - 懒加载方法
- (CLGeocoder *)geoCoder
{
    if (!_geoCoder) {
        _geoCoder = [[CLGeocoder alloc] init];
    }
    return _geoCoder;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值