Map View
Map View
很常在尋找定位或是路線導覽中看到,接著簡單示範Map View的操作。
該範例使用Segmented Contrl
來切換Map View
的樣式,並且定位出目前的所在位址(如下第三張圖)。



**Step 1.**建立Single View
專案,專案名稱為Map View
**Step 2.**編輯MainStoryboard.storyboard
加入:
- Toolbar
- Segmented Control
- Bar Button Item
- Flexible Space Bar Button Item
- Map View

**Step 3.**加入MapKit
的framework
在檔案瀏覽的地方點選專案,接著畫面中間選擇TARGETS
在往右邊上方找到Build Phases
,下方可看到Link Binary With Libraries
,點選下方的MapKit.framework
加進來。加入成功以後在你的專案資料夾內即可看到一個MapKit.framework

**Step 4.**編輯ViewController.h
- 引入
MapKit/MapKit.h
- 定義變數,如同程式碼所述
- 定義兩個
IBAction
,該用途如程式碼的註解
1 |
|
**Step 5.**編輯ViewController.m
一樣要先補上synthesize
,接著是撰寫showLocation:
,該方法很簡單,只要定義mapView
的showsUserLocation
為YES即可顯示目前該使用者的位址。
1 | @synthesize mapView = mapView_, segmented = segmented_; |
在改變地圖的模式,使用Segmented Control
,操作的方式如同Segmented Control,唯一不同的是在case
內執行的東西;在case
內去改變mapView
的mapType
,地圖模式一共有三種模式可以更換,因此您可以依自己的需求去做變換。
1 | -(IBAction)changeMapType:(id)sender |
**Step 6.**建立關連
