特定location的pinpoint实际上是一个ui,对应着一个名为MKAnnotation的protocal,需要声明三个属性:

经纬度坐标coordinate,标题title,以及子标题subtitle。

1.我们定义一个MyAnnotation的类去为存储pinpoint所需的属性,即coordinate,title,subtitle

   MyAnnotation.h文件:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject <MKAnnotation>

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

- (id) initWithCoordinates: (CLLocationCoordinate2D)paramCoordinates
                     title: (NSString *)paramTitle
                  subtitle: (NSString *)paramSubtitle;

@end

   MyAnnotation.m文件:

#import "MyAnnotation.h"

@implementation MyAnnotation

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates title:(NSString *)paramTitle subtitle:(NSString *)paramSubtitle
{
    self = [super init];
    if (self != nil) {
        coordinate = paramCoordinates;
        title = paramTitle;
        subtitle = paramSubtitle;
    }
    return self;
}

@end

2.通过在MKMapView中调用addAnnotation函数,将对应的pinpoint插入到地图中

[self.mapView addAnnotation:annotation];

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    self.myMapView.mapType = MKMapTypeHybrid;
    self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.myMapView];
    
    MyAnnotation* annotation = [[MyAnnotation alloc] initWithCoordinates:CLLocationCoordinate2DMake(10.2432456, 20.456423) title:@"China" subtitle:@"this is not China"];
    [self.myMapView addAnnotation:annotation];
}

CLLocationCoordinate2DMake(float, float);        // 设置经度和纬度

3.结果显示为:

 

note that:

  可以通过[self.myMapView setShowsUserLocation:YES]来定位到当前用户的所在位置