1. import Header File
#import <MapKit/MapKit.h>#import <CoreLocation/CoreLocation.h>
2. Add gesture recognition for member variable Mapview
1 -(void) viewdidload2{3 4 Add gesture recognition 5 [Self.mapview Addgesturerecognizer:[[uitapgesturerecognizer Alloc] initwithtarget:self action: @selector (Tapmapview:)]; 6 }
3. Achieving the target approach
1- (void) Tapmapview: (UITapGestureRecognizer *) Tap2 {3 //gets the x, y coordinates of the clicked View4Cgpoint point =[Tap LocationInView:tap.view];5 //x, y coordinates to geographic coordinates6cllocationcoordinate2d coordinate =[Self.mapview convertpoint:point ToCoordinateFromView:tap.view];7 //The pin is initialized because the system's Mkannotation properties are read-only8Qkannotation *annotation =[[Qkannotation alloc]init];9 //set the PIN propertiesTenAnnotation.title =@"my pin."; OneAnnotation.subtitle =@"My pin-sub-title"; AAnnotation.coordinate =coordinate; - //add a pin on the map - [Self.mapview addannotation:annotation]; the}
3.1 System Mkannotation Properties, all read-only
1 #import<CoreGraphics/CoreGraphics.h>2 #import<CoreLocation/CoreLocation.h>3 #import<MapKit/MKFoundation.h>4 5 @protocolMkannotation <NSObject>6 7 //Center latitude and longitude of the annotion view.8 //The implementation of the must be KVO compliant.9@property (Nonatomic,ReadOnly) cllocationcoordinate2d coordinate;Ten One @optional A - //Title and subtitle for use by selection UI. -@property (Nonatomic,ReadOnly, copy) NSString *title; the@property (Nonatomic,ReadOnly, copy) NSString *subtitle; - - //called as a result of dragging an annotation view. -- (void) Setcoordinate: (cllocationcoordinate2d) newcoordinate ns_available (10_9, 4_0); + - @end
4. The. h file of the custom pin, the. m file does not need to be modified
1 #import <MapKit/MapKit.h>23@interface qkannotation:nsobject< Mkannotation>45@property (nonatomic,assign) cllocationcoordinate2d coordinate; 6 @property (nonatomic, copy) NSString *title; 7 @property (nonatomic, copy) NSString *subtitle; 8 9 @end
#注意: 1.MKAnnotation is NSObject, it is a property of Mkannotationview
2.MKAnnotationView inherits from UIView
3. Custom pins inherit from NSObject. By complying with the <MKAnnotation> agreement, the properties and methods stated in the agreement are obtained. From 3.1 of the code can be seen, the system Mkannotation object, the entire. h file is a protocol, coordinate is the property that must be implemented, Delete the readonly and add the assign to the coordinate property.
Basic use of IOS pins