#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
Tags: import 經緯度 where 標簽
文章來源: