#import "ViewController.h"
#import "MyAnnotation.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 打开地图
[self addMapView];
// 调用自带导航
// [self useMapNavigation];
}
- (void)addMapView {
// 创建地图视图 初始化参数
self.map = [[MKMapView alloc] initWithFrame:[self.view bounds]];
// 是否显示当前位置
self.map.showsUserLocation = YES;
// 地图类型
self.map.mapType = MKMapTypeHybrid;
// 定义经纬度 coordinate [kəʊ'ɔ:dɪneɪt]n. 坐标
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(39.915352, 116.397105);
// 定义一个float控制显示的范围
float zoomLevel =0.02;
// 定义一个显示区域 第一个参数是经纬度 第二个参数是范围 region ['riːdʒ(ə)n]n. 地区;范围;
MKCoordinateRegion region = MKCoordinateRegionMake(coords, MKCoordinateSpanMake(zoomLevel, zoomLevel));
// 地图上显示这个区域
[self.map setRegion:region animated:YES];
// 标注对象 annotation
MyAnnotation *annotation = [[MyAnnotation alloc] init];
// 标签标题
annotation.title = @"where?";
// 标签副标题
annotation.subtitle = @"The Imperial Place";
// 打头针的位置
annotation.coordinate = coords;
// 在地图上添加大头针
[self.map addAnnotation:annotation];
// 将地图添加到视图上
[self.view addSubview:self.map];
}
- (void)useMapNavigation {
// *调用app自带导航,需要传入一个数组和一个字典,数组中放入MKMapItem,字典中放入对应键值
// 标注对象
MyAnnotation *longGe = [[MyAnnotation alloc] init];
// 标签标题
longGe.title = @"where?";
// 标签副标题
longGe.subtitle = @"Here is 崇文门";
// 打头针的位置
longGe.coordinate = CLLocationCoordinate2DMake(39.90, 116.42);
// 在地图上添加大头针
[self.map addAnnotation:longGe];
// 标注对象2
MyAnnotation *study = [[MyAnnotation alloc] init];
// 标签标题
study.title = @"where?";
// 标签副标题
study.subtitle = @"购物广场";
// 打头针的位置
study.coordinate = CLLocationCoordinate2DMake(40.0306, 116.3435);
// 在地图上添加大头针
[self.map addAnnotation:study];
// 将地图添加到视图上
//建立字典存储导航的相关参数
NSMutableDictionary *md = [NSMutableDictionary dictionary];
md[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving;
md[MKLaunchOptionsMapTypeKey] = [NSNumber numberWithInteger:MKMapTypeHybrid];
//获取到起点的MKplaceMark
// MKPlacemark对象存储placemark数据对于一个给定的纬度和经度。地标数据包括信息,如国家,国家,城市,街道地址与指定的坐标。
MKPlacemark *startPlace = [[MKPlacemark alloc] initWithCoordinate:longGe.coordinate addressDictionary:md];
// 获取到终点的MKplaceMark,MKPlaceMark 是ClPlaceMark的子类。
MKPlacemark *endPlace = [[MKPlacemark alloc] initWithCoordinate:study.coordinate addressDictionary:md];
// MKMapItem类封装了地图上的一个特定点的信息。这些信息包括地图位置和任何其他数据可能相关的
// 将MKPlaceMark转换成MKMapItem,这样可以放入到item这个数组中
MKMapItem *startItem = [[MKMapItem alloc ] initWithPlacemark:startPlace];
MKMapItem *endItem = [[MKMapItem alloc ] initWithPlacemark:endPlace];
NSArray *item = @[startItem ,endItem];
// #warning 其实所有的代码都是为了下面一句话,打开系统自带的高德地图然后执行某些动作,launchOptions里面的参数指定做哪些动作
[MKMapItem openMapsWithItems:item launchOptions:md];
}
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
iOS MKMapView简单介绍
最新推荐文章于 2018-05-01 13:00:47 发布