MKMapView学习

一、MKMapView的基本使用

#import <MapKit/MapKit.h>
@interface ViewController (){
    CLLocationManager *manger ;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    MKMapView *mapview =[[MKMapView alloc]init];
    mapview.frame = self.view.bounds;
    
    mapview.mapType = MKMapTypeStandard;
    
    mapview.showsCompass = YES; // 是否显示指南针
    mapview.showsScale = YES; // 是否显示比例尺
    mapview.showsTraffic = YES; // 是否显示交通
    mapview.showsBuildings = YES; // 是否显示建筑物
  
    [self.view addSubview:mapview];
}
@end

二、在地图上显示用户的位置

显示用户的位置,就会涉及到定位。如果要显示用户位置就需要主动请求用户授权,自然你需要先去infoplist文件中配置定位的权限。
1、方式一

#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController (){
    CLLocationManager *Locatonmanger;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    MKMapView *mapview =[[MKMapView alloc]init];
    mapview.frame = self.view.bounds;
    [self.view addSubview:mapview];
    //1、主动请求位置授权方式
    Locatonmanger = [[CLLocationManager alloc]init];
    [Locatonmanger requestAlwaysAuthorization];
    
    //2、在地图上显示w用户位置
    mapview.showsUserLocation = YES;
  
}

@end

效果:会在地图上显示一个蓝点, 标识用户所在位置; 但地图不会缩放, 而且当用户位置移动时, 地图不会跟随用户位置移动而移动

2、方式二

#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController (){
    CLLocationManager *Locatonmanger;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    MKMapView *mapview =[[MKMapView alloc]init];
    mapview.frame = self.view.bounds;
    [self.view addSubview:mapview];
    //1、主动请求位置授权方式
    Locatonmanger = [[CLLocationManager alloc]init];
    [Locatonmanger requestAlwaysAuthorization];
    
    //2、设置用户追踪模式
    mapview.userTrackingMode= MKUserTrackingModeFollow;
  
}

@end

效果: 会在地图上显示一个蓝点, 标识用户所在位置; 而且地图缩放到合适比例,显示用户位置, 当用户位置移动时, 地图会跟随用户位置移动而移动; 但是有时候失效;

三、获取位置信息

#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>{
    CLLocationManager *Locatonmanger;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    MKMapView *mapview =[[MKMapView alloc]init];
    mapview.delegate = self;
    mapview.frame = self.view.bounds;
    [self.view addSubview:mapview];
    //1、主动请求位置授权方式
    Locatonmanger = [[CLLocationManager alloc]init];
    [Locatonmanger requestAlwaysAuthorization];
    
    //2、设置用户追踪模式
    mapview.userTrackingMode= MKUserTrackingModeFollow;
  
}

#pragma mark - 获取用户位置信息
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    
     CGFloat   longtitude = userLocation.coordinate.longitude;
     CGFloat   latitud   = userLocation.coordinate.latitude;
    
     CLGeocoder *gecoder = [[CLGeocoder alloc]init];
     CLLocation *location = [[CLLocation alloc]initWithLatitude:latitud longitude:longtitude];
    
    [gecoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *placemark=[placemarks firstObject];
        
        NSDictionary *dic = placemark.addressDictionary;
        NSLog(@"详细信息:%@",dic);
    }];
    
}
@end

MKUserLocation为大头针数据模型,只要遵守了MKAnnotation协议就可以作为大头针数据模型。

四、大头针的使用

在地图上操作大头针,实际上是控制大头针数据模型: 添加大头针就是添加大头针数据模型; 删除大头针就是删除大头针数据模型。

1、在地图上添加大头针视图
 - (void)addAnnotation:(id <MKAnnotation>)annotation;

2、在地图上 删除大头针视图
- (void)removeAnnotation:(id <MKAnnotation>)annotation;
- (void)removeAnnotations:(NSArray<id<MKAnnotation>> *)annotations;
  • 4.1 、创建一个 大头针数据模型
    系统提供的MKUserLocation类就是一个大头针数据模型
#import <MapKit/MKFoundation.h>
#import <MapKit/MKAnnotation.h>
@class CLLocation;
@class MKUserLocationInternal;
@interface MKUserLocation : NSObject <MKAnnotation>

// Returns YES if the user's location is being updated.
@property (readonly, nonatomic, getter=isUpdating) BOOL updating;

// Returns nil if the owning MKMapView's showsUserLocation is NO or the user's location has yet to be determined.
@property (readonly, nonatomic, nullable) CLLocation *location;

// Returns nil if not in MKUserTrackingModeFollowWithHeading
@property (readonly, nonatomic, nullable) CLHeading *heading NS_AVAILABLE(10_9, 5_0) __TVOS_PROHIBITED;

// The title to be displayed for the user location annotation.
@property (nonatomic, copy, nullable) NSString *title;

// The subtitle to be displayed for the user location annotation.
@property (nonatomic, copy, nullable) NSString *subtitle;

@end

然而location这里是readonly,这样我们就无法愉快地创建。
因此需要新建一个自定义的大头针数据模型。

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface MyMKUserLocation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;//插在地图上的位置
@property (nonatomic, copy, nullable) NSString *title;
@property (nonatomic, copy, nullable) NSString *subtitle;
@end

#import "MyMKUserLocation.h"
@implementation MyMKUserLocation
@end

//鼠标点击在地图哪个位置, 就在对应的位置添加一个大头针, 并在标注弹框中显示对应的城市和街道;


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //1. 获取触摸点在地图上对应的坐标
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.mapview];
    
    //2. 将坐标转换成为经纬度
    CLLocationCoordinate2D coordinate = [self.mapview convertPoint:touchPoint toCoordinateFromView:self.mapview];
    
    //3. 根据经纬度创建大头针数据模型, 并添加在地图上
    MyMKUserLocation *myLocation = [[MyMKUserLocation alloc] init];
    myLocation.coordinate = coordinate;
    myLocation.title = @"中国";
    myLocation.subtitle = @"你好";
    [self.mapview addAnnotation:myLocation];
 
}
  • 4.2 、自定义大头针视图
  1. 如果想要自定义大头针, 必须使用 MKAnnotationView 或者 自定义的子类
  2. 但是不能直接使用系统默认的大头针, 会无效
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "MyMKUserLocation.h"

@interface ViewController ()<MKMapViewDelegate>{
    CLLocationManager *Locatonmanger;
    MKUserLocation *_userLocation;
}
@property(nonatomic,strong)MKMapView *mapview;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    MKMapView *mapview =[[MKMapView alloc]init];
    mapview.delegate = self;
    mapview.frame = self.view.bounds;
    [self.view addSubview:mapview];
    //1、主动请求位置授权方式
    Locatonmanger = [[CLLocationManager alloc]init];
    [Locatonmanger requestAlwaysAuthorization];
    
    //2、设置用户追踪模式
    mapview.userTrackingMode =   MKUserTrackingModeFollow;
  
    self.mapview = mapview;
}


/**
 如果方法没有实现,或者返回的是nil,那么系统就会使用默认 的大头针视图,显示到地图上

 @param mapView 地图
 @param annotation 大头针数据模型
 @return 大头针视图 MKAnnotationView系统大头针视图对应的类
 */
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    //大头针,也是有“循环利用机制”
    
    static NSString *ide = @"Mk";
     MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:ide];
    if (annotationView == nil) {
        annotationView  = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:ide];
    }
    
    annotationView.annotation = annotation;
    // 设置大头针图片
    annotationView.image = [UIImage imageNamed:@"category_3"];
    
    // 设置大头针可以弹出标注
   annotationView.canShowCallout = YES;
    
    // 设置标注左侧视图
    UIImageView *leftIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    leftIV.image = [UIImage imageNamed:@"huba.jpeg"];
    annotationView.leftCalloutAccessoryView = leftIV;
    
    // 设置标注右侧视图
    UIImageView *rightIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    rightIV.image = [UIImage imageNamed:@"eason.jpg"];
    annotationView.rightCalloutAccessoryView = rightIV;
    
    // 设置标注详情视图(iOS9.0)
    annotationView.detailCalloutAccessoryView = [[UISwitch alloc] init];
    return annotationView;

}
@end

猜你喜欢

转载自blog.csdn.net/weixin_33804582/article/details/87223239
今日推荐