Add points on the map view to indicate these geographical locations.
Solution:
Use the built-in annotation of the map. Follow these steps:
1. Create a new class named MyAnnotation.
2. Make sure that this class implements the MKAnnotation protocol.
3. Define a CLLocationCoordinate2D attribute for this class and name it coordinate. Note that this parameter must be marked as read-only. Because the Coordinate defined in the MKAnnotation protocol is also read-only.
4. Similarly, two NSString attributes are defined, named title and subtitle respectively. These two parameters are used to save the title and content information of the anchor.
5. Add an initialization method for this class. This method needs to receive a parameter of the CLLocationCoordinate2D type. In this method, the attribute defined in step 3 is passed in. Because this attribute is read-only, it cannot be assigned outside of this class. Therefore, this initialization method is a bridge that enables us to pass values to this class normally. Similarly, the title and subtitle operations must be similar.
6. Initialize the MyAnnotation class and add it to your map. Use the Annotation method.
As described in the solution, we must create an object that complies with the MKAnnotation protocol, instantiate the object, and pass it to the map for display. . H
# Import <Foundation/Foundation. h> # import <MapKit/MapKit. h> @ interface MyAnnotation: NSObject <MKAnnotation> // coordinate [k ?? '? : D? Ne? T] coordinate annotation [& aelig; n? 'Te ?? (?) N] annotation @ property (nonatomic, readonly)CLLocationCoordinate2DCoordinate; @ property (nonatomic, copy, readonly) NSString * title; @ property (nonatomic, copy, readonly) NSString * subtitle; // initialization method-(id) initWithCoordinates :( CLLocationCoordinate2D) paramCoordinates title :( NSString *) paramTitle subTitle :( NSString *) paramSubTitle; @ end
. M
# Import "MyAnnotation. h "@ implementation MyAnnotation-(id) initWithCoordinates :( CLLocationCoordinate2D) paramCoordinates title :( NSString *) paramTitle subTitle :( NSString *) paramSubTitle {self = [super init]; if (self! = Nil) {_ coordinate = paramCoordinates; _ title = paramTitle; _ subtitle = paramSubTitle;} return self ;}@ endThen instantiate the class and add it to our map. For example, write the following code in the view controller implementation file:
-(Void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; self. myMapView = [[MKMapView alloc] initWithFrame: self. view. bounds]; self. myMapView. mapType = MKMapTypeStandard; self. myMapView. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self. myMapView. delegate = self; [self. view addSubview: _ myMapView]; // add an anchor to the map // Create a simple position degrees angle. The degree is latitude longitude CLLocationCoordinate2D location =CLLocationCoordinate2DMake(35.8219, 116.0225); // Create the annotation using the locationMyAnnotation * annotation = [[MyAnnotation alloc] initWithCoordinates: location title: @ "My Title" subTitle: @ "My Sub Title"];// Add the anchor point to the map [_ myMapViewAddAnnotation:Annotation];}
2. Add anchors of different colors
To customize the color displayed for our anchor in the map view (select the anchor color provided by the SDK ), we must obtain a MKPinAnnotationView instance object instead of the previous MKAnnotationView object. We need to use the mapView: viewForAnnotation method to obtain this instance object. Remember that MKPinAnnotationView is a subclass of MKAnnotationView. For specific implementation methods, see the following code:
Map2: add an anchor to a map to add an anchor of different colors.