繼續(xù)上一盤開始用Swift開發(fā)iOS 10 - 14 基礎(chǔ)動畫舒萎,模糊效果和Unwind Segue,這一篇使用地圖。
添加MapKit
框架:
添加Map到應(yīng)用中
效果圖如下:
- 在Detail view的table view底部添加一個Map View。高度設(shè)置為135杂靶,取消一些屬性
zooming
,scrolling
,rotating
等,使地圖沒有交互功能酱鸭。
刪除之前去掉table view底部的代碼吗垮,讓底部顯示。
tableView.tableFooterView = UIView(frame: CGRect.zero)
添加新的視圖控制器凹髓,并在其中添加Map View烁登,調(diào)整大小為全屏。
control-drag從detail視圖控制器到新的地圖視圖控制器蔚舀,選擇show饵沧。因為table view的頭部和底部是不能選擇的,所有不能從table view的底部control-drag到地圖視圖控制器赌躺。
- 為了能檢測的table view底部map的是否被接觸狼牺,需要為地圖添加
UITapGestureRecognizer
。
在RestaurantDetailViewController.swift
中引入import MapKit
;
定義地圖接口@IBOutlet var mapView: MKMapView!
礼患,并關(guān)聯(lián);
在viewDidLoad
中添加:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showMap))
mapView.addGestureRecognizer(tapGestureRecognizer)
另外添加方法:
func showMap() {
performSegue(withIdentifier: "showMap", sender: self)
}
用Geocoder轉(zhuǎn)換地址到經(jīng)緯度
類似下面:
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString("上海東方明珠", completionHandler: {
placemarks, error in
for p in placemarks! {
print(p.location?.coordinate)
}
})
placemarks
是CLPlacemark
的數(shù)組是钥。
地圖標(biāo)注(annotation)介紹
通過地址文本獲得經(jīng)緯度后就可在地圖上標(biāo)注指示掠归,類似下面的樣子:
地圖標(biāo)注的代碼一般如下,先通過地址文本生成的經(jīng)緯度悄泥,規(guī)定MKPointAnnotation
的經(jīng)緯度虏冻,然后把MKPointAnnotation
添加到地圖視圖中即可。
let annotation = MKPointAnnotation()
if let location = placemark.location {
annotation.coordinate = location.coordinate
mapView.addAnnotation(annotation)
}
為沒有交互的地圖添加標(biāo)注
在RestaurantDetailViewController.swift
的viewDidLoad
中添加:
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(restaurant.location, completionHandler: {
placemarks, error in
if error != nil {
print(error)
return
}
if let placemarks = placemarks {
let placemark = placemarks[0]
let annotation = MKPointAnnotation()
if let location = placemark.location {
annotation.coordinate = location.coordinate
self.mapView.addAnnotation(annotation)
// 規(guī)定地圖顯示半徑 250米
let region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 250, 250)
self.mapView.setRegion(region, animated: false)
}
}
})
為全屏的地圖添加標(biāo)注
- 新建一個視圖控制器
MapViewController
弹囚,關(guān)聯(lián)全屏地圖控制器厨相,引入地圖框架import MapKit
。 - 第一個地圖接口和
restaurant
變量
@IBOutlet var mapView: MKMapView!
var restaurant:Restaurant!
- 更新
viewDidLoad
:
override func viewDidLoad() {
super.viewDidLoad()
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(restaurant.location, completionHandler: {
placemarks, error in
if error != nil {
print(error)
return
}
if let placemarks = placemarks {
// Get the first placemark
let placemark = placemarks[0]
// 1
let annotation = MKPointAnnotation()
annotation.title = self.restaurant.name
annotation.subtitle = self.restaurant.type
if let location = placemark.location {
annotation.coordinate = location.coordinate
// 2
self.mapView.showAnnotations([annotation], animated: true)
self.mapView.selectAnnotation(annotation, animated: true)
}
}
})
}
- 1 設(shè)置
MKPointAnnotation
一些屬性 - 2 在地圖展示標(biāo)注余寥。地圖上課展示很多標(biāo)注领铐。
selectAnnotation:
方法是標(biāo)注顯示被選中的樣式悯森。
在標(biāo)注中添加圖片
- 讓
MapViewController
實現(xiàn)MKMapViewDelegate
協(xié)議宋舷,并在viewDidLoad
中設(shè)置mapView.delegate = self
- 添加
mapView(_:viewFor:)
方法,當(dāng)?shù)貓D每次需要annotation時調(diào)用:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"
// 1
if annotation.isKind(of: MKUserLocation.self) {
return nil
}
// 2
var annotationView: MKPinAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
}
// 3
let leftIconView = UIImageView(frame: CGRect(x: 0, y: 0, width: 53, height: 53))
leftIconView.image = UIImage(named: restaurant.image)
annotationView?.leftCalloutAccessoryView = leftIconView
// 4
annotationView?.pinTintColor = UIColor.orange
return annotationView
}
- 1 判斷是否用戶當(dāng)前位置瓢姻。用戶當(dāng)前位置算是一種特殊的標(biāo)注祝蝠,是當(dāng)前位置,就不需要另外添加標(biāo)注了幻碱。
- 2 創(chuàng)建
MKPinAnnotationView
绎狭。有點類似創(chuàng)建table view cell。 - 3 在
MKPinAnnotationView
上添加圖片褥傍。 - 4 改變標(biāo)注針的顏色儡嘶。
地圖定制
mapView.showsCompass = true
mapView.showsScale = true
mapView.showsTraffic = true
showsCompass
表示在右上角顯示羅盤。
showsScale
表示在左上角顯示縮放比例尺恍风。
showsTraffic
表示顯示交通信息蹦狂。
代碼
Beginning-iOS-Programming-with-Swift
說明
此文是學(xué)習(xí)appcode網(wǎng)站出的一本書 《Beginning iOS 10 Programming with Swift》 的一篇記錄
系列文章目錄
- 開始用Swift開發(fā)iOS 10 - 1 前言
- 開始用Swift開發(fā)iOS 10 - 2 Hello World!第一個Swift APP
- 開始用Swift開發(fā)iOS 10 - 3 介紹Auto Layout
- 開始用Swift開發(fā)iOS 10 - 4 用Stack View設(shè)計UI
- [開始用Swift開發(fā)iOS 10 - 5 原型的介紹]
- 開始用Swift開發(fā)iOS 10 - 6 創(chuàng)建簡單的Table Based App
- 開始用Swift開發(fā)iOS 10 - 7 定制Table Views
- 開始用Swift開發(fā)iOS 10 - 8 Table View和UIAlertController的交互
- 開始用Swift開發(fā)iOS 10 - 9 Table Row的刪除, UITableViewRowAction和UIActivityViewController的使用
- 開始用Swift開發(fā)iOS 10 - 10 Navigation Controller的介紹和Segue
- 開始用Swift開發(fā)iOS 10 - 11 面向?qū)ο缶幊探榻B
- 開始用Swift開發(fā)iOS 10 - 12 豐富Detail View和定制化Navigation Bar