首先,我们得新建一个遵守MKAnnotation的对象,并且拥有以下几个属性:
@interface LHAnnotation : NSObject<MKAnnotation>
// Center latitude and longitude of the annotion view.
// The implementation of this property must be KVO compliant.
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
// Title and subtitle for use by selection UI.
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
然后了,在需要它的地方导入这个对象,LHAnnotation,就行了。
下面是主要的代码(纯代码,没有使用storyboard):
// Created by 妖精的尾巴 on 15-8-13.
// Copyright (c) 2015年 妖精的尾巴. All rights reserved.
//
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "LHAnnotation.h"
@interface ViewController ()<MKMapViewDelegate>
@property(nonatomic,strong)MKMapView* mapView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setMap];
LHAnnotation* annotation=[[LHAnnotation alloc]init];
annotation.coordinate=CLLocationCoordinate2DMake(39.924115, 116.186063);
annotation.title=@"石景山区";
annotation.subtitle=@"领袖大厦B座";
[self.mapView addAnnotation:annotation];
}
-(void)setMap
{
self.mapView=[[MKMapView alloc]init];
self.mapView.frame=self.view.bounds;
/**
*mapType
*
* MKMapTypeStandard = 0,
MKMapTypeSatellite,
MKMapTypeHybrid
*/
self.mapView.mapType=MKMapTypeStandard;
/**
*Set to YES to add the user location annotation to the map and start updating its location
*/
self.mapView.showsUserLocation=NO;
/**
*Zoom and scroll are enabled by default.
*/
self.mapView.scrollEnabled=YES;
self.mapView.zoomEnabled=YES;
/**
*Rotate and pitch are enabled by default on Mac OS X and on iOS 7.0 and later.
*/
self.mapView.pitchEnabled=YES;
self.mapView.rotateEnabled=YES;
[self.view addSubview:self.mapView];
}
运行程序截图如下