地图

地图

1.#import "ViewController.h"
2.#import <MapKit/MapKit.h>
3.
4.@interface ViewController () <CLLocationManagerDelegate, MKMapViewDelegate> {
5.
6. // 苹果原生的地图视图
7. MKMapView *myMapView;
8.
9. // 定位服务管理器
10. CLLocationManager *myLocManager;
11.}
12.
13.@end
14.
15.@implementation ViewController
16.
17.- (void)viewDidLoad {
18. [super viewDidLoad];
19.
20. myMapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
21. // 为MKMapView绑定委托
22. myMapView.delegate = self;
23.
24. [self.view addSubview:myMapView];
25.
26. // 创建经纬度坐标的结构体变量
27. // CLLocationCoordinate2DMake函数的第一个参数是纬度第二个参数是经度
28. CLLocationCoordinate2D c2d = CLLocationCoordinate2DMake(30.662221 , 104.041367);
29. // 指定地图缩放的比例1=111km
30. MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
31. // 创建一个区域结构体变量
32. MKCoordinateRegion region = MKCoordinateRegionMake(c2d, span);
33. // 设置地图中心为指定区域
34. [myMapView setRegion:region animated:YES];
35.
36. // 创建定位服务管理器对象
37. myLocManager = [[CLLocationManager alloc] init];
38. myLocManager.delegate = self;
39. [myLocManager requestAlwaysAuthorization];
40. [myLocManager startUpdatingLocation];
41.
42. // 显示用户的位置
43. myMapView.showsUserLocation = YES;
44.
45. // 创建一个长按手势识别器
46. UILongPressGestureRecognizer *r = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(createPin:)];
47. // 在地图上添加长按手势
48. [myMapView addGestureRecognizer:r];
49.
50. // 创建一个地理信息编码器
51. CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
52. // 创建一个位置信息对象
53. CLLocation *loc = [[CLLocation alloc] initWithLatitude:c2d.latitude longitude:c2d.longitude];
54.
55. // 地理信息反编码
56. [geoCoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) {
57. if (!error) {
58. CLPlacemark *pMark = [placemarks firstObject];
59. NSData *data = [NSJSONSerialization dataWithJSONObject:pMark.addressDictionary options:NSJSONWritingPrettyPrinted error:nil];
60. NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
61.
62. // 使用官方地图自带的应用
63.
64. // 创建地图上的一个位置
65. MKPlacemark *mkMark = [[MKPlacemark alloc] initWithPlacemark:pMark];
66. // 创建一个地图应用项(以指定的位置进行导航、查周边等操作)
67. MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:mkMark];
68. // 设置使用标准地图应用
69. NSDictionary *options = @{MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard)};
70. // 启动地图应用项(启动苹果官方地图应用)
71. [mapItem openInMapsWithLaunchOptions:options];
72. }
73. else {
74. NSLog(@"%@", error);
75. }
76. }];
77.}
78.
79.// 长按手势的回调方法(创建一个大头针)
80.- (void) createPin:(UILongPressGestureRecognizer *) sender {
81. if (sender.state == UIGestureRecognizerStateBegan) {
82. // 获得长按的位置在窗口视图上的横纵坐标
83. CGPoint point = [sender locationInView:self.view];
84. // 将视图上的横纵坐标转换成经纬度坐标
85. CLLocationCoordinate2D c2d = [myMapView convertPoint:point toCoordinateFromView:self.view];
86.
87. // 创建大头针(标注)的数据模型(此处不创建视图,视图通过MKMapView的委托设置回调方法来生成的)
88. MKPointAnnotation *ann = [[MKPointAnnotation alloc] init];
89.
90. // 指定大头针的经纬度坐标(位置)以及附加的信息
91. ann.coordinate = c2d;
92. ann.title = @"这是一个标注";
93. ann.subtitle = @"这是标注的说明";
94.
95. // 将大头针数据模型添加到MKMapView上管理
96. [myMapView addAnnotation:ann];
97. }
98.}
99.
100.// 当地图上添加了标注以后要执行的回调方法
101.- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
102. // 获取可复用的大头针视图
103. MKPinAnnotationView *pinView = (id)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN"];
104.
105. if (!pinView) { // 如果没有可重用的大头针视图就创建并指定重用标识
106. pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN"];
107. }
108.
109. // 以气泡方式显示大头针的信息
110. pinView.canShowCallout = YES;
111. // 启用大头针掉下动画
112. pinView.animatesDrop = YES;
113.
114. UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
115. headerImageView.layer.cornerRadius = 20;
116. headerImageView.layer.masksToBounds = YES;
117. headerImageView.image = [UIImage imageNamed:@"7.jpg"];
118. // 定制大头针气泡信息的左侧附加视图
119. pinView.leftCalloutAccessoryView = headerImageView;
120.
121. UIButton *goButton = [UIButton buttonWithType:UIButtonTypeCustom];
122. goButton.frame = CGRectMake(0, 0, 40, 40);
123. [goButton setTitle:@"▶️" forState:UIControlStateNormal];
124. [goButton addTarget:self action:@selector(goButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
125. // 定制大头针气泡信息的右侧附加视图
126. pinView.rightCalloutAccessoryView = goButton;
127.
128. return pinView;
129.}
130.
131.- (void) goButtonClicked:(UIButton *) sender {
132. NSLog(@"OK");
133.}
134.
135.// 定位的位置发生改变后的回调方法
136.- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
137. // 从数组中取出第一个元素(包括当前的定位信息[经纬度、海拔、速度、方向等])
138. CLLocation *location = [locations firstObject];
139. // 创建一个新的区域
140. MKCoordinateRegion newRegion = MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.1, 0.1));
141. // 将地图位置设置到新的区域
142. [myMapView setRegion:newRegion animated:YES];
143.}
144.
145.@end
146.
 
原文地址:https://www.cnblogs.com/buakaw/p/5211388.html