標籤:
首先在工程中匯入MapKit.framework庫檔案
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 : UIViewController4 5 @end
1 #import "RootViewController.h" 2 #import <MapKit/MapKit.h> 3 #import "YXYCAnation.h" 4 5 @interface RootViewController ()<MKMapViewDelegate> 6 7 @end 8 9 @implementation RootViewController10 11 - (void)viewDidLoad {12 [super viewDidLoad];13 // 初始化MKMapView14 MKMapView *mapView = [[MKMapView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];15 // 設定代理16 mapView.delegate = self;17 // 設定顯示當前位置18 mapView.showsUserLocation = YES;19 // 設定地圖顯示類型20 mapView.mapType = MKMapTypeStandard;21 // 經緯度22 CLLocationCoordinate2D coord2D = {23.117000,113.27500};23 // 顯示範圍精度24 MKCoordinateSpan span = {0.01,0.01};25 // 顯示地區26 MKCoordinateRegion region = {coord2D,span};27 // 給地圖設定顯示地區28 [mapView setRegion:region animated:YES];29 [self.view addSubview:mapView];30 31 // 建立anation對象32 CLLocationCoordinate2D showCoord = {23.117000,113.27500};33 YXYCAnation *anation1 = [[YXYCAnation alloc] initWithCoordinate2D:showCoord];34 anation1.title = @"越秀公園";35 anation1.subtitle = @"小標題";36 [mapView addAnnotation:anation1];37 }38 39 #pragma mark -MKAnnotationView delegate-40 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation41 {42 static NSString *identify = @"Annotian";43 MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identify];44 if (annotationView == nil) {45 // MKPinAnnotationView 是圖釘視圖46 annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identify];47 // 設定是否顯示標題視圖48 annotationView.canShowCallout = YES;49 50 UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];51 [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];52 annotationView.rightCalloutAccessoryView = button;53 }54 annotationView.pinColor = MKPinAnnotationColorRed;55 annotationView.animatesDrop = YES;56 57 return annotationView;58 }59 60 - (void)buttonAction:(UIButton *)sender61 {62 NSLog(@"顯示公園簡介");63 }64 65 @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
如下:
iOS 地圖(添加圖釘)