1.MapKit Framework use Premise ① import frame
② importing the primary header file
#import <MapKit/MapKit.h>
③mapkit Framework Usage Notes
- The prefix for all data types in the Mapkit framework is MK
- Mapkit has a more important UI control, specifically for map display
The ④mapkit framework contains CLLocation2. Set the type of the map you can set the map type by setting the Maptype of the Mkmapview
typedef enum : NSUInteger { MKMapTypeStandard , 标准(默认) MKMapTypeSatellite ,卫星 MKMapTypeHybrid 混合(标准 + 卫星) } MKMapType;
3. Tracking the user's location, the map enlarged display
注意:
In IOS8, if you want to track a user's location, you must voluntarily request privacy permissions
- Cllocationmanager request Authorization in the Cllocation framework
- Using Mapkit to get the user's location, you can track
/* typedef NS_ENUM(NSInteger, MKUserTrackingMode) { MKUserTrackingModeNone = 0, 不追踪/不准确的 MKUserTrackingModeFollow, 追踪 MKUserTrackingModeFollowWithHeading, 追踪并且获取用的方向 } */ self.mapView.userTrackingMode = MKUserTrackingModeFollow;
4. Set up agent to implement proxy method
self.mapView.delegate = self;
Proxy method: Called when the area of the ① map is complete
- 在此方法中可以得到用户的当前位置的mapView的中心点和经纬度跨度
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ MKCoordinateRegion region = mapView.region; // 经纬度跨度 MKCoordinateSpan span = region.span; NSLog(@"latitudeDelta = %f", span.latitudeDelta); NSLog(@"longitudeDelta = %f", span.longitudeDelta);}
② Common proxy methods are
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation;//一个位置更改默认只会调用一次,不断监测用户的当前位置每次调用,都会把用户的最新位置(userLocation参数)传进来- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated;//地图的显示区域即将发生改变的时候调用- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;//地图的区域改变完成时调用
Set the map display area, and latitude and longitude span ① by Mkmapview The following methods, you can set the location and area of the map display
// 设置地图的中心点位置@property (nonatomic) CLLocationCoordinate2D centerCoordinate;- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated;// 设置地图的显示区域@property (nonatomic) MKCoordinateRegion region;- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
②mkcoordinateregion
//MKCoordinateRegion是一个用来表示区域的结构体,定义如下typedef struct { CLLocationCoordinate2D center; // 区域的中心点位置 MKCoordinateSpan span; // 区域的跨度} MKCoordinateRegion;//MKCoordinateSpan的定义typedef struct { CLLocationDegrees latitudeDelta; // 纬度跨度 CLLocationDegrees longitudeDelta; // 经度跨度} MKCoordinateSpan;
Example: Return to current position
//1. Get the latitude and longitude of the user cllocationcoordinate2d usercoordinate = self.mapview.userLocation.coordinate; //2. Set the center point coordinate of the map is the latitude and longitude of the user //2.1 The first setting method //[Self.mapview setcentercoordinate:usercoordinate Animated:yes]; //2.2 The second setting method /* span can be called when the area of the map is changed-(void) Mapview: ( Mkmapview *) Mapview regiondidchangeanimated: (BOOL) animated; */Mkcoordinatespan span = {1.0, 1.0}; Mkcoordinateregion region = {usercoordinate, span}; [self.mapview setregion:region animated:yes";
5. Pin
- To identify a specific object in the location (e.g., a restaurant in this position).
Basic operation of ① pin
// 添加一个大头针- (void)addAnnotation:(id <MKAnnotation>)annotation;// 添加多个大头针- (void)addAnnotations:(NSArray *)annotations;// 移除一个大头针- (void)removeAnnotation:(id <MKAnnotation>)annotation;// 移除多个大头针- (void)removeAnnotations:(NSArray *)annotations;
(ID) What is the annotation parameter? Pin Model object: Data used to encapsulate a pin, such as the position of a pin, title, sub-title, etc. ② PIN model
- Any model object that adheres to the agreement
- To rewrite the value of the variable in the protocol
属性
, override these variables
- Here
属性
, just to define the get and set methods, not the so-called traditional properties
/** * 大头针的位置 */@property (nonatomic, assign) CLLocationCoordinate2D coordinate;/** * 大头针标题 */@property (nonatomic, copy) NSString *title;/** * 大头针的子标题 */@property (nonatomic, copy) NSString *subtitle;
③ Add a PIN create a PIN model and assign a value to a member variable
// 创建大头针模型 YLAnnotation *anno = [[YLAnnotation alloc] init]; anno.title = @"广东海洋大学"; anno.subtitle = @"广东省湛江市广东海洋大学"; CGFloat latitude = 24.00; CGFloat longitude = 103.76; anno.coordinate = CLLocationCoordinate2DMake(latitude , longitude);
Add a PIN to the map
[self.customMapView addAnnotation:anno];
6. Custom Pin ① is called each time a PIN is added (several pins on the map are called several times)
/* * @param mapView 地图 * @param annotation 大头针模型 * * @return 大头针的view*/- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation;
Note: If nil is returned, the system will display its own default mode
Create a PIN, attach a model property to a PIN
StaticNSString *identifier =@ "Anno";1. Take Mkannotationview *annoview = [Mapview Dequeuereusableannotationviewwithidentifier:identifier] from the cache pool;2. If not in the cache pool, create a newif (Annoview = =Nil) {Annoview = [[Mkannotationview alloc] initwithannotation:annotation reuseidentifier:identifier];//set the PIN header to show //custom pins, by default clicking on the pin will not show the title, we need to manually set the display Annoview.canshowcallout = yes; //Set pin header display offset bit annoview.calloutoffset = Cgpointmake (-< Span class= "Hljs-number" >50, 0); //set pin to the left of the auxiliary view Annoview.leftcalloutaccessoryview = [[ Uiswitch alloc] init]; //set pin to the right of the auxiliary view Annoview.rightcalloutaccessoryview = [uibutton Buttonwithtype:uibuttontypecontactadd]; } //set the pin's picture Annoview.image = [ UIImage imagenamed:@ "Category_4"];
Create another medium pin
- Sub-class of Mkannotationview Mkpinannotationview
- The new added effect from the day drop, and
StaticNSString *identifier =@ "Anno";1. Take Mkpinannotationview *annoview = (Mkpinannotationview *) from the cache pool [Mapview Dequeuereusableannotationviewwithidenti Fier:identifier];2. If not in the cache pool, create a newif (Annoview = =Nil) {Annoview = [[Mkpinannotationview alloc] Initwithannotation:Nil Reuseidentifier:identifier];Set the color of the pin Annoview. Pincolor = Mkpinannotationcolorpurple;Set a pin down from the sky Annoview. Animatesdrop =YES;Sets whether the PIN header is displayedCustom PIN, by default, click the pin will not show the title, we need to manually set the display Annoview. Canshowcallout =YES;//Set PIN header to display offset bit annoview. Calloutoffset = Cgpointmake (-- 0); //Set the secondary view to the left of the pin Annoview. Leftcalloutaccessoryview = [[Uiswitch alloc] init]; //Set PIN to the right of the auxiliary view Annoview. Rightcalloutaccessoryview = [UIButton buttonwithtype:uibuttontypecontactadd];} //Set the PIN's picture //Note: If you are using a custom pin created by Mkpinannotationview, the settings picture is not valid because there are some actions inside the system that override our own settings Annoview. Image = [UIImage imagenamed:@ "Category_4"]; //4. Return pin view return annoview;
Custom Annotationview
- Type Custom cell
- Inherit Mkannotationview
This method is called each time a PIN is added-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (id<mkannotation>) annotation{//NSLog (@ "%s", __func__); NSLog (@ "annotation = = =%@", annotation); //PIN for the user's current position special handling if ([Annotation iskindofclass:[hmannotation class]] = = NO) { return nil;} //1. Create a custom pin Ylannotationview *annoview = [Ylannotationview Annotationviewwithmap:mapview]; //2. Set the model (can not assign value, the system will also automatically execute this method)//annoview.annotation = annotation; //3. Return the PIN to return Annoview;}
Custom Annotationview method
Initialization method + (Instancetype) Annotationviewwithmap: (Mkmapview *) mapview{StaticNSString *identifier =@ "Anno";1. Take Ylannotationview *annoview = (Ylannotationview *) from the cache pool [Mapview dequeuereusableannotationviewwithidentifier: Identifier];//2. If no cache pool is created, create a new if (Annoview = = nil) {//parameter is nil because Setannotation: The method assigns a value to the annotation model Annoview = [[ Ylannotationview alloc] initwithannotation:nil reuseidentifier:identifier]; } return annoview;} //-(void) Setannotation: (id<mkannotation>) annotation//This method is overriding the Set method-(void) Setannotation: ( Ylannotation *) annotation{[Super setannotation:annotation]; Handle your own unique operation self. image = [UIImage imagenamed:annotation. icon];}
iOS map----Mapkit frame