首页 > 代码库 > Displaying Pins on a Map View

Displaying Pins on a Map View

Problem

You want to point out a specific location on a map to the user.

Solution

Use built-in map view annotations. Follow these steps:

1. Create a new class and call it MyAnnotation.

2.Make sure this class conforms to the MKAnnotation protocol.

3.Define a property for this class of type CLLocationCoordinate2D and name it coordinate. Make sure you set it as a readonly property since the coordinate property is defined as readonly in the MKAnnotation protocol.

4.Optionally, define two properties of type NSString, namely title and subtitle, which will be able to carry the title and the subtitle information for your annotation view. Both of these properties are readonly as well.

5.Create an initializer method for your class that will accept a parameter of type CLLocationCoordinate2D. In this method, assign the passed location parameter to the property that we defined in step 3. Since this property is readonly, it cannot be assigned by code outside the scope of this class. Therefore, the initializer of this class acts as a bridge here and allows us to indirectly assign a value to this property. We will do the same thing for the title and subtitle properties.

6.Instantiate the MyAnnotation class and add it to your map using the addAnnota tion: method of the MKMapView class.

 

Discussion

As explained in this recipe’s Solution, we must create an object that conforms to the MKAnnotation protocol and later instantiate this object and pass it to the map to be displayed. We will declare the header of this object like so:

 

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

 

@interface WSYAnnotation : NSObject<MKAnnotation>

 

@property(nonatomic,readonly)CLLocationCoordinate2D coordinate;

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

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

 

- (instancetype)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates

                              title:(NSString *)paramTitle

                           subTitle:(NSString *)paramSubTitle;

 

@end

 

The .m file of the MyAnnotation class sets up the class to display location information as follows:

#import "WSYAnnotation.h"

 

@implementation WSYAnnotation

 

-(instancetype)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates title:(NSString *)paramTitle subTitle:(NSString *)paramSubTitle

{

    self = [self init];

   

    if (self!=nil) {

        _coordinate = paramCoordinates;

        _title = paramTitle;

        _subtitle = paramSubTitle;

    }

    return self;

}

@end

 

Later, we will instantiate this class and add it to our map, for instance, in the .m file of a view controller that creates and displays a map view:

#import "WSYViewController.h"

#import <MapKit/MapKit.h>

#import "WSYAnnotation.h"

 

@interface WSYViewController ()<MKMapViewDelegate>

@property(nonatomic,strong)MKMapView * myMapView;

 

@end

 

@implementation WSYViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

   

    //Create a map as big as our view

   

    self.myMapView = [[MKMapView alloc]initWithFrame:self.view.bounds];

   

    self.myMapView.delegate = self;

   

    //set the map type to standard

    self.myMapView.mapType = MKMapTypeStandard;

   

    self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

   

    //add it to our view

    [self.view addSubview:self.myMapView];

   

    //this is just a simple loaction

    CLLocationCoordinate2D location = CLLocationCoordinate2DMake(27, 117);

   

    //create the annotation uisng the location

    WSYAnnotation * annotation = [[WSYAnnotation alloc]initWithCoordinates:location title:@"my title" subTitle:@"My sub Title"];

   

    //add eventually add it to the map

    [self.myMapView addAnnotation:annotation];

   

}

@end