码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 地图(添加大头针)

时间:2015-08-17 23:19:05      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:

首先在工程中导入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 : UIViewController
4 
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 RootViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     // 初始化MKMapView
14     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>)annotation
41 {
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 *)sender
61 {
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 地图(添加大头针)

标签:

原文地址:http://www.cnblogs.com/lantu1989/p/4737864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!