1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 5 @property (strong, nonatomic) UIWindow *window; 6 7 @end
1 #import "AppDelegate.h" 2 #import "RootViewController.h" 3 @interface AppDelegate () 4 5 @end 6 7 @implementation AppDelegate 8 9 10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 11 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 12 // Override point for customization after application launch. 13 self.window.backgroundColor = [UIColor whiteColor]; 14 15 self.window.rootViewController = [[RootViewController alloc] init]; 16 17 [self.window makeKeyAndVisible]; 18 return YES; 19 } 20 21 @end
1 #import <UIKit/UIKit.h> 2 3 @interface RootViewController : UIViewController 4 5 @end
1 #import "RootViewController.h" 2 #import <MapKit/MapKit.h> 3 #import "YXYCAnation.h" 4 @interface RootViewController ()<MKMapViewDelegate> 5 6 @end 7 8 @implementation RootViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 // 初始化MKMapView 13 MKMapView *mapView = [[MKMapView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 14 // 设置代理 15 mapView.delegate = self; 16 // 设置显示当前位置 17 mapView.showsUserLocation = YES; 18 // 设置地图显示类型 19 mapView.mapType = MKMapTypeStandard; 20 // 经纬度 21 CLLocationCoordinate2D coord2D = {23.117000,113.27500}; 22 // 显示范围精度 23 MKCoordinateSpan span = {0.01,0.01}; 24 // 显示区域 25 MKCoordinateRegion region = {coord2D,span}; 26 // 给地图设置显示区域 27 [mapView setRegion:region animated:YES]; 28 [self.view addSubview:mapView]; 29 30 // 创建anation对象 31 CLLocationCoordinate2D showCoord = {23.117000,113.27500}; 32 YXYCAnation *anation1 = [[YXYCAnation alloc] initWithCoordinate2D:showCoord]; 33 anation1.title = @"越秀公园"; 34 anation1.subtitle = @"小标题"; 35 [mapView addAnnotation:anation1]; 36 } 37 38 #pragma mark -MKAnnotationView delegate- 39 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 40 { 41 static NSString *identifier = @"Annotion"; 42 MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 43 if (annotationView == nil) { 44 annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotationView reuseIdentifier:identifier]; 45 annotationView.image = [UIImage imageNamed:@"0"]; 46 } 47 return annotationView; 48 } 49 50 - (void)buttonAction:(UIButton *)sender 51 { 52 NSLog(@"显示公园简介"); 53 } 54 55 @end
1 #import <Foundation/Foundation.h> 2 #import <MapKit/MapKit.h> 3 @interface YXYCAnation : NSObject<MKAnnotation> 4 5 @property (nonatomic, assign) CLLocationCoordinate2D coordinate; 6 @property (nonatomic, copy) NSString *title; 7 @property (nonatomic, copy) NSString *subtitle; 8 9 - (id)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate; 10 11 @end
1 #import "YXYCAnation.h" 2 3 @implementation YXYCAnation 4 5 - (id)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate 6 { 7 self = [super init]; 8 if (self != nil) { 9 _coordinate = coordinate; 10 } 11 return self; 12 } 13 14 15 @end