The display and positioning of Mapview is explained in the previous section, which shows how to place a pin on a map and set a view of the click Pin.
"System default PIN"
The way to place a pin on a mapview is to call its addannotation: method, and note that the model is passed in instead of the pin view.
-(void) Addannotation: (id <MKAnnotation>) annotation;
With this approach, we can clearly see that the annotation model needs to follow the Mkannotation protocol, and we open this Protocol, and we can see something like this:
@protocol mkannotation <nsobject>//Center latitude and longitude of the annotation view.//the implementation of th Is property must was KVO compliant. @property (nonatomic, readonly) cllocationcoordinate2d coordinate; @optional//Title and Subtitle for use by Selection UI. @property (nonatomic, readonly, copy) NSString *title; @property (Nonatomic, ReadOnly, CO PY) NSString *subtitle;//called as a result of dragging an annotation view.-(void) Setcoordinate: (cllocationcoordinate2d) Newcoordinate ns_available (10_9, 4_0); @end
So we should define a model that defines these properties according to the protocol, where coordinate must be defined because the PIN must have a position.
The model class follows the Mkannocation protocol, named Myannotation, as follows:
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface myannotation:nsobject< mkannotation>/** * pin position * * @property (nonatomic, assign) cllocationcoordinate2d coordinate;/** * pin header */@ Property (nonatomic, copy) nsstring *title;/** * Pin's sub-title */@property (nonatomic, copy) NSString *subtitle; @end
#import "MyAnnotation.h" @implementation myannotation@end
You can then add a pin by defining the model, calling Addannotation: method, for example, we add a pin randomly by button:
-(Ibaction) Addpin: (ID) Sender { myannotation *anno = [[Myannotation alloc] init]; Anno.title = @ "title"; Anno.subtitle = @ "subtitle"; Cllocationdegrees latitude = 36.821119 + arc4random_uniform (); Cllocationdegrees longtitude = 116.750112 + arc4random_uniform (); Anno.coordinate = Cllocationcoordinate2dmake (latitude, longtitude); [Self.mapview Addannotation:anno];}
The title and subtitle are the titles and subheadings on the bubbles that appear when you click a PIN.
The pin display principle and TableView display cell is similar, once called Addannotation: method, will call Mapview:viewforannotationview: method, this method should take out a pin view from the cache pool, Or if a new PIN view is not created in the cache pool, then return.
The difference from TableView is that if nil is returned directly, the PIN will be returned according to the system's style.
Note that if you do not return nil, to deal with different types of pins, do not forget the user's position indicates that the dot is also a pin, if only the setting of the PIN, even the user location will be a pin instead of a dot.
The decision method is simple, because the user position pin is created by the system, so the incoming model is certainly not our own model myannotation, as long as the incoming model is not the class, if not, then return nil, representing the system by default style, create a dot.
To create a pin, use the Mkpinannotationview, if the use of Mkannotationview, is not with the picture, can not be displayed directly.
-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (id<mkannotation>) annotation{static NSS Tring *id = @ "Anno"; If nil is returned, the system will be displayed by default, and if customized, it cannot be displayed directly, and no caption will be displayed after clicking the pin, you need to manually set the display//If you want to display it directly, you should call Mkpinannotationview Mkpinannotationview *annoview = (Mkpinannotationview *) [Mapview Dequeuereusableannotationviewwithidentifier:id]; if (Annoview = = nil) {Annoview = [[Mkpinannotationview alloc] Initwithannotation:nil Reuseidentifier:id]; Pin attribute Annoview.animatesdrop = YES; Set the animation of the drop of the pin annoview.canshowcallout = YES; Sets whether the click Pin shows bubbles Annoview.calloutoffset = Cgpointmake (0, 0); Sets the offset of the pin bubble//sets the left and right view of the pin bubble, can be any uiview annoview.leftcalloutaccessoryview = [UIButton buttonwithtype:uibut Tontypecontactadd]; Annoview.rightcalloutaccessoryview = [[Uiswitch alloc] init]; [Annoview Setpincolor:mkpinannotationcolorpurple]; Set the PIN color}//Set the picture of the pin, if the mkpin is created directly, it is not valid. Annoview.image = [UIImage imagenamed:@ "pin"]; annoview.annotation = annotation; If it is a user location, the default dot should be displayed instead of a pin, so it should be judged if it is myannotation if (![ Annoview.annotation Iskindofclass:[myannotation class]) {return nil; } return Annoview; }
"Custom Pin"
Custom pins need to use Mkannotationview, you specify the image, no fall animation, no other difference.
(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (id<mkannotation>) annotation{static NSSt Ring *id = @ "Anno"; If nil is returned, the system will be displayed by default, and if customized, it cannot be displayed directly, and no caption will be displayed after clicking the pin, you need to manually set the display//If you want to display it directly, you should call Mkpinannotationview Mkannotationview *annoview = [Mapview dequeuereusableannotationviewwithidentifier:id]; if (Annoview = = nil) {Annoview = [[Mkpinannotationview alloc] Initwithannotation:nil Reuseidentifier:id]; Pin attribute//annoview.animatesdrop = YES; Mkpinannotaionview only effective, set the pin drop animation annoview.canshowcallout = YES; Sets whether the click Pin shows bubbles Annoview.calloutoffset = Cgpointmake (0, 0); Sets the offset of the pin bubble//sets the left and right view of the pin bubble, can be any uiview annoview.leftcalloutaccessoryview = [UIButton buttonwithtype:uibut Tontypecontactadd]; Annoview.rightcalloutaccessoryview = [[Uiswitch alloc] init]; [Annoview Setpincolor:mkpinannotationcolorpurple]; Mkpinannotaionview only valid, set the color of the pin}//Set the picture of the pin Annoview.imaGE = [UIImage imagenamed:@ "pin"]; annoview.annotation = annotation; If it is a user location, the default dot should be displayed instead of a pin, so it should be judged if it is myannotation if (![ Annoview.annotation Iskindofclass:[myannotation class]) {return nil; } return Annoview; }
"The MVC encapsulation Annotationview's attention point"
annotation to pass in the model, in the overridden set method, notice the call to the parent class's set method, set the public property, and then set its own unique.
In one detail, the incoming model of the default set method is the Id<mkannotaion> type, which requires a strong-turn type when used, and for convenience we change the type directly to Myannotation, because the message that OC sends when invoking the method includes only the method name and colon, the description, Therefore, the type is not part of the method name and can be modified.
-(void) Setannotation: (myannotation *) annotation{ [Super setannotation:annotation]; Set unique properties, examples, left and right views, and so on. }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(80) Mapkit placing system default pins and custom pins