iOS Development Mapkit

Source: Internet
Author: User

1 , overview

Mapkit Framework Usage Prerequisites:

Import Frame:

To import 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: Mkmapview, which is designed for map display.

2 , Trace Displays the user's location

Set the Usertrackingmode property of Mkmapview to track the current location of the user:

Mkusertrackingmodenone: Do not track the user's location

Mkusertrackingmodefollow: Track and display the user's current location on the map

Mkusertrackingmodefollowwithheading: Tracks and displays the user's current location on the map, which rotates with the user's forward direction

Is the tracking effect:

The blue Glow dot is the user's current position.

Blue Glow origin, the professional term is called "pin".

3 , type of Map

You can set the map type by setting the Mkmapview Mapviewtype:

Mkmaptypestandard: General map (left)

Mkmaptypesatellite: Satellite cloud Image (Central)

Mkmaptypehybrid: General map over satellite cloud image (right)

4 , Mkmapview the agent

Mkmapview can set up a proxy object to listen to the map's related behavior.

The common proxy methods are:

-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation:

(Mkuserlocation *) userlocation;

Very frequent calls, constantly monitoring the user's current location

Each invocation, the user's latest position (userlocation parameter) is passed in.

-(void) Mapview: (Mkmapview *) Mapview regionwillchangeanimated:

(BOOL) animated;

Called when the map's display area is about to change

-(void) Mapview: (Mkmapview *) Mapview regiondidchangeanimated:

(BOOL) animated;

Called when the display area of the map has changed

5 , mkuserlocation

Mkuserlocation is actually a PIN model that includes the following properties.

@property (nonatomic, copy) NSString *title;

Title displayed on the Pin

@property (nonatomic, copy) NSString *subtitle;

Sub-headings displayed on a PIN

@property (readonly, nonatomic) cllocation *location;

Location information

6 , set the display of the map

You can set the location and area of the map display by Mkmapview the following methods.

Set the center point location of the map:

@property (nonatomic) cllocationcoordinate2d centercoordinate;

-(void) Setcentercoordinate: (cllocationcoordinate2d) Coordinate animated: (BOOL) animated;

To set the display area of a map:

@property (nonatomic) Mkcoordinateregion region;

-(void) Setregion: (mkcoordinateregion) region animated:

(BOOL) animated;

For example:

-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation:

(Mkuserlocation *) userlocation

{

Userlocation.title = @ "The Imperial Park of the Kingdom";

Userlocation.subtitle = @ "is a great place to be!" ";

Cllocationcoordinate2d Center = userLocation.location.coordinate;

Set the display range of a map

Mkcoordinatespan span = Mkcoordinatespanmake (0.021321, 0.019366);//The smaller the map, the more precise the location

Mkcoordinateregion region = Mkcoordinateregionmake (center, span);//Set Map center point to the location currently anchored to

[Mapview setregion:region Animated:yes];

}

7 , mkcoordinateregion Structural Body

mkcoordinateregion is a structure used to represent a region, defined as follows:

typedef struct {

Cllocationcoordinate2d Center; The center point location of the area

Mkcoordinatespan span; span of Area

} mkcoordinateregion;

Mkcoordinatespan definition of:

typedef struct {

Cllocationdegrees Latitudedelta; Latitude span

Cllocationdegrees Longitudedelta; Longitude span

} Mkcoordinatespan;

8 , basic operation of a PIN

Add a PIN

-(void) Addannotation: (id <MKAnnotation>) annotation;

Add Multiple Pins

-(void) Addannotations: (Nsarray *) annotations;

Remove a PIN

-(void) Removeannotation: (id <MKAnnotation>) annotation;

Remove multiple Pins

-(void) Removeannotations: (Nsarray *) annotations;

(ID <MKAnnotation>) 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.

9 , pin model

Create a new PIN model class

#import <MapKit/MapKit.h>

@interface Mjtuangouannotation:nsobject <MKAnnotation>

/** Coordinate position */

@property (nonatomic, assign) cllocationcoordinate2d coordinate;

/** Title */

@property (nonatomic, copy) NSString *title;

/** Sub-title */

@property (nonatomic, copy) NSString *subtitle;

@end

Ten , add a pin

Mjtuangouannotation *anno = [[Mjtuangouannotation alloc] init];

Anno.title = @ "This is the position";

Anno.subtitle = @ "This is the location description";

Anno.coordinate = Cllocationcoordinate2dmake (40, 116);

[Self.mapview Addannotation:anno];

For example, click on the screen Mkmapview where to add a pin:

First, to comply with the Proxy protocol: mkmapviewdelegate

-(void) viewdidload

{

[Super Viewdidload];

1. Track the user's location (shows the user's location)

Self.mapView.userTrackingMode = Mkusertrackingmodefollow;

2. Setting up the agent

Self.mapView.delegate = self;

3. Listen for Mapview click events

[Self.mapview Addgesturerecognizer:[[uitapgesturerecognizer alloc] initwithtarget:self action: @selector (TapMapView :)]];

}

/**

* Monitor Mapview's clicks

*/

-(void) Tapmapview: (UITapGestureRecognizer *) tap

{

1. Get the user's position in the Mapview click (x, y)

Cgpoint point = [Tap LocationInView:tap.view];

2. Convert mathematical coordinates to geographic latitude and longitude coordinates

cllocationcoordinate2d coordinate = [Self.mapview convertpoint:point ToCoordinateFromView:self.mapView];

3. Create a PIN model and add a PIN to the map

Mjannotation *anno = [[Mjannotation alloc] init];

Anno.coordinate = coordinate;//Sets the latitude and longitude coordinates of a pin

Anno.title = @ "This is the position";

Anno.subtitle = @ "This is the location description";

[Self.mapview addannotation:anno];//Add a PIN

}

Because the system comes with the pin mkannotation properties are read-only, can not change its properties, that is, cannot set its position, title, etc., the solution is to customize the mkannotation, rewrite its properties, the need to use its properties to be writable. The method is to create a new generic class mjannotation, so that it complies with the Mkannotation protocol and imports the frame header file <mapkit/mapkit.h> Then override the properties in the MJAnnotation.h file without moving the mjannotation.m file. As follows:

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface Mjannotation:nsobject <MKAnnotation>

@property (nonatomic, assign) cllocationcoordinate2d coordinate;

Originally for @property (nonatomic, readonly) cllocationcoordinate2d coordinate;

@property (nonatomic, copy) NSString *title;

Originally for @property (nonatomic, readonly, copy) NSString *title;

@property (nonatomic, copy) NSString *subtitle;

Originally for @property (nonatomic,readonly copy) NSString *subtitle;

@end

One , Custom pins

In many cases, you need to customize the display style of a pin, such as displaying a picture:

Custom PIN steps:

First step: Custom Pins ( mkannotation )

The MJTuangouAnnotation.h file is as follows (no need to move. m file):

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface Mjtuangouannotation:nsobject <MKAnnotation>

@property (nonatomic, assign) cllocationcoordinate2d coordinate;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *subtitle;

@property (nonatomic, copy) NSString *icon; This property is not the system comes with, we manually added

@end

Step Two: Keep the controller compliant mkmapviewdelegate protocol, and set the controller to Mkmapview the agent

Self.mapView.delegate = self;

Step three: Implement the following proxy method to return the pin control

-(Mkannotationview *) Mapview: (Mkmapview *) Mapview

Viewforannotation: (id <MKAnnotation>) annotation;

For example:

-(void) viewdidload

{

[Super Viewdidload];

1. Food

Mjtuangouannotation *anno0 = [[Mjtuangouannotation alloc] init];

Anno0.title = @ "xxx hotel";

Anno0.subtitle = @ "Full 15 percent, member 20 percent";

Anno0.coordinate = Cllocationcoordinate2dmake (39, 115);

Anno0.icon = @ "Eat";

[Self.mapview Addannotation:anno0];

2. Film

Mjtuangouannotation *anno1 = [[Mjtuangouannotation alloc] init];

Anno1.title = @ "XXX Cinema";

Anno1.subtitle = @ "Latest blockbuster: Captain America 2, coming soon ... ";

Anno1.coordinate = Cllocationcoordinate2dmake (29, 110);

Anno1.icon = @ "movie";

[Self.mapview Addannotation:anno1];

Self.mapView.delegate = self;//Set Agent

}

-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (id<mkannotation>) annotation

{

if (![ Annotation Iskindofclass:[mjtuangouannotation class]) return nil;// to allow the custom pin to coexist with the system default PIN, if you do not add this line of code, a property not found by the system icon property will appear when locating, because this property is not included in the system and is manually added.

static NSString *id = @ "Tuangou";

Remove a pin view that can be recycled from the cache pool

Mkannotationview *annoview =

[Mapview Dequeuereusableannotationviewwithidentifier:id];

if (Annoview = = nil) {

Annoview=[[mkannotationview Alloc] Initwithannotation:

annotation Reuseidentifier:id];

Set whether title and sub-headings are displayed

Annoview.canshowcallout = YES;

Set the offset of the pin description

Annoview.calloutoffset = Cgpointmake (0,-10);

Set the pin description to the right of the control

Annoview.rightcalloutaccessoryview =

[UIButton Buttonwithtype:uibuttontypecontactadd];

Set the pin description to the left of the control

Annoview.leftcalloutaccessoryview =

[UIButton Buttonwithtype:uibuttontypedetaildisclosure];

}

Delivery model

annoview.annotation = annotation;

To set the picture, manually set the picture instead of manually setting the caption because the caption is a property of its own, and icon is the property we added.

Mjtuangouannotation *tuangouanno = annotation;

Annoview.image = [UIImage ImageNamed:tuangouAnno.icon];

Return annoview;//returns NIL displays the default Pin

}

Fourth step: According to the passing in (id <MKAnnotation>) annotation parameter to create and return the corresponding PIN control.

Note The use of the agent method:

If nil is returned, the displayed pin takes the default style of the system.

A blue glowing dot that identifies a user's location, which is also a pin, and the proxy method is invoked when the pin is displayed.

Therefore, you need to be clear in the Proxy method (ID <MKAnnotation>) The annotation parameter represents a custom pin or a blue glow dot.

A , Mkannotationview

The pin control on the map is Mkannotationview.

Properties of the Mkannotationview:

@property (nonatomic, strong) id <MKAnnotation> annotation;

Pin model

@property (nonatomic, strong) UIImage *image;

The picture shown

@property (nonatomic) BOOL canshowcallout;

Whether the callout is displayed

@property (nonatomic) Cgpoint calloutoffset;

Offset of the callout

@property (Strong, nonatomic) UIView *rightcalloutaccessoryview;

What controls appear to the right of the callout

@property (Strong, nonatomic) UIView *leftcalloutaccessoryview;

What controls appear to the left of the callout

11. Mkpinannotationview

Mkpinannotationview is a subclass of Mkannotationview.

Mkpinannotationview has 2 more properties than Mkannotationview:

@property (nonatomic) Mkpinannotationcolor Pincolor;

Pin Color

@property (nonatomic) BOOL Animatesdrop;

Whether the pin was dropped from the sky the first time

iOS Development Mapkit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.