CYC-MKMapView用法

需要导入的几个框架

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface ViewController ()<MKMapViewDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic,strong) MKMapView *mapView;
@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation ViewController
- (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 *placemack = [placemarks firstObject];
        // 经度
        CGFloat longitude = placemack.location.coordinate.longitude;
        // 维度
        CGFloat  latitude = placemack.location.coordinate.latitude;
        NSLog(@"地理位置!!!!! ~~维度%f  经度%f", latitude, longitude);
    }];


    // 地图初始化
    self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

    // 设置代理
    self.mapView.delegate = self;

    // 设置地图跟随移动
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;

    // 设置地图样式
    self.mapView.mapType = MKMapTypeStandard;


    [self.view addSubview:self.mapView];

}
#pragma mark - 代理方法

// 定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations firstObject];
     NSLog(@"%@", location);

    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) {
            NSLog(@"%@", [error localizedDescription]);
        } else {

        CLPlacemark *placemark = [placemarks firstObject];
        NSLog(@"%@", placemark.addressDictionary);

        }

    }];

}


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


#pragma mark - 懒加载方法
- (CLGeocoder *)geocoder
{
    if (!_geocoder) {
        _geocoder = [[CLGeocoder alloc] init];
    }

    return _geocoder;
}


这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值