iOS Development Mapkit

Source: Internet
Author: User

use of the Mapkit framework

Import Frame

Import 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, specifically for map display

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, professional term called " pin " .

type of Map

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

Mkmaptypestandard : General map (left)

Mkmaptypesatellite : satellite cloud Image (Central)

Mkmaptypehybrid : General map over satellite cloud image (right)

Mkmapview 's agent

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

The common proxy methods are

1 -(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 .

1 -(void) Mapview: (Mkmapview *) Mapview regionwillchangeanimated: (BOOL) animated;

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

1 -(void) Mapview: (Mkmapview *) Mapview regiondidchangeanimated: (BOOL) animated;

Called when the display area of the map has changed

mkuserlocation

Mkuserlocation is actually a PIN model that includes the following properties

1 @property (nonatomic, copy) NSString *title;

Title displayed on the Pin

1 @property (nonatomic, copy) NSString *subtitle;

Sub-headings displayed on a PIN

1 @property (readonly, nonatomic) cllocation *location;  

Location information (where are the pins pinned ?)

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

1 @property (nonatomic) cllocationcoordinate2d centercoordinate; 2 3 -(void) Setcentercoordinate: (cllocationcoordinate2d) Coordinate animated: (BOOL) animated;

Set the display area of a map

1 @property (nonatomic) Mkcoordinateregion region; 2 3 -(void) Setregion: (mkcoordinateregion) region animated: (BOOL) animated;
mkcoordinateregion

Mkcoordinateregion is a structure used to represent a region, defined as

1 struct {23       // the center point location of the area 4 5       // span of Area 6 7 } mkcoordinateregion;

Mkcoordinatespan the definition

1 struct {23     // Latitude span 4 5     // Longitude span 6 7 } Mkcoordinatespan;
Pin

What is a pin?

A PIN in real life

A pin on the map

To identify a specific object in the location (e.g., a restaurant in this position).

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.

PIN Model

Create a new PIN model class

1 #import<MapKit/MapKit.h>2 3 @interfaceMjtuangouannotation:nsobject <MKAnnotation>4 /** Coordinate position*/5 @property (nonatomic, assign) cllocationcoordinate2d coordinate;6 /** Title*/7@property (nonatomic, copy) NSString *title;8 /** Sub-title*/9@property (nonatomic, copy) NSString *subtitle;Ten @end

Add a PIN

1 mjtuangouannotation *anno = [[Mjtuangouannotation alloc] init]; 2 @" What do you think? " ; 3 @" I look in the window. " ; 4 anno.coordinate = Cllocationcoordinate2dmake (in); 5 [Self.mapview Addannotation:anno];

Custom Pins

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

Custom Pins

How to customize a pin

Set up a proxy for Mkmapview

Implement the following proxy method, which returns the Pin control

-(mkannotationview *) Mapview: (mkmapview *) Mapview viewforannotation: (ID < Mkannotation>) annotation;

Create and return the corresponding PIN control based on the passed in (ID <mkannotation>) Annotation parameter

Use of proxy methods Note

If nil is returned , the displayed pin takes the system's default style

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

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

1-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (ID<MKAnnotation>) Annotation2 {3     //determine the type of annotation4     if(! [Annotation iskindofclass:[mjtuangouannotationclass]])returnNil;5     6     //Create Mkannotationview7     StaticNSString *id =@"Tuangou";8Mkannotationview *annoview =[Mapview Dequeuereusableannotationviewwithidentifier:id];9     if(Annoview = =Nil) {TenAnnoview =[[Mkannotationview alloc] initwithannotation:annotation reuseidentifier:id]; OneAnnoview.canshowcallout =YES; A     } -     //Passing model data -Annoview.annotation =annotation; the      -     //set up a picture -Mjtuangouannotation *tuangouannotation =annotation; -Annoview.image =[UIImage ImageNamed:tuangouAnnotation.icon]; +      -     returnAnnoview; +}
Mkannotationview

The pin control on the map is Mkannotationview

Properties of the Mkannotationview

1@property (nonatomic, Strong)ID<MKAnnotation>annotation;2 PIN Model3 4@property (nonatomic, strong) UIImage *image;5 the picture shown6 7 @property (nonatomic) BOOL canshowcallout;8 whether the callout is displayed9 Ten @property (nonatomic) cgpoint calloutoffset; One offset of the callout A  -@property (Strong, nonatomic) UIView *Rightcalloutaccessoryview; - what controls appear to the right of the callout the  -@property (Strong, nonatomic) UIView *Leftcalloutaccessoryview; -What controls appear to the left of the callout
Mkpinannotationview

Mkpinannotationview is a subclass of Mkannotationview

Mkpinannotationview 2 more properties than Mkannotationview

1 @property (nonatomic) Mkpinannotationcolor Pincolor; 2 Pin Color 3 4 @property (nonatomic) BOOL Animatesdrop; 5 The first time a PIN is displayed, does it fall from the sky

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.