這篇文章有以下三個(gè)知識(shí)點(diǎn)
- 設(shè)置地圖類型
- 在地圖上面顯示用戶位置
- 設(shè)置用戶大頭針信息
顯示地圖:
在storyboard中拖個(gè)MKMapView或是用代碼創(chuàng)建個(gè)MKMapView添加到視圖中就可以了膏孟。
注意import MapKit,不然會(huì)報(bào)錯(cuò)。
設(shè)置地圖類型
設(shè)置地圖類型只需設(shè)置MKMapView的mapType屬性拌汇。
open var mapType: MKMapType // 地圖類型
MKMapType是一個(gè)結(jié)構(gòu)體
public enum MKMapType : UInt {
case standard //標(biāo)準(zhǔn)類型
case satellite // 衛(wèi)星類型
case hybrid // 標(biāo)準(zhǔn)和衛(wèi)星結(jié)合的混合類型
}
MKMapType枚舉還有一些其他的值柒桑,但是目前在中國(guó)還沒有支持,了解下就可以了噪舀。
下面是mapType屬性的使用代碼和效果圖
@IBOutlet weak var mapView: MKMapView!
/// 切換segmentController值變化的時(shí)候調(diào)用這個(gè)方法
///
/// - Parameter sender: UISegmentedControl
@IBAction func segmentControlValueChange(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
mapView.mapType = .standard // 用標(biāo)準(zhǔn)類型
case 1:
mapView.mapType = .satellite // 用衛(wèi)星類型
case 2:
mapView.mapType = .hybrid // 用標(biāo)準(zhǔn)和衛(wèi)星混合的類型
default:
break
}
}
標(biāo)準(zhǔn)類型效果圖:
衛(wèi)星類型效果圖:
混合類型效果圖:
提示:
衛(wèi)星類型和混合類型不仔細(xì)看的話沒太大區(qū)別魁淳,放大地圖看的話會(huì)有很大區(qū)別
衛(wèi)星類型用的比較少,因?yàn)榭床坏匠鞘械刃畔ⅰ?/p>
以上更改地圖類型的代碼可以簡(jiǎn)潔為一句
/// 切換segmentController值變化的時(shí)候調(diào)用這個(gè)方法
///
/// - Parameter sender: UISegmentedControl
@IBAction func segmentControlValueChange(_ sender: UISegmentedControl) {
mapView.mapType = MKMapType(rawValue: UInt(sender.selectedSegmentIndex))!
}
在地圖上面顯示用戶位置
要顯示用戶位置与倡,需要兩步
- 獲取用戶位置授權(quán)
- 設(shè)置MKMapView的userTrackingMode屬性
獲取用戶位置授權(quán)在我之前的文章寫過了界逛。淺談iOS定位,這里就不啰嗦了纺座。
userTrackingMode屬性
open var userTrackingMode: MKUserTrackingMode // 用戶追蹤模式
MKUserTrackingMode是個(gè)結(jié)構(gòu)體
public enum MKUserTrackingMode : Int {
case none // 不獲取用戶位置
case follow // 獲取用戶位置不顯示方向信息
case followWithHeading // 獲取用戶位置顯示方向信息
}
實(shí)例代碼:
@IBOutlet weak var mapView: MKMapView!
var manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// 請(qǐng)求用戶授權(quán)
manager.requestWhenInUseAuthorization()
// 獲取用戶位置不顯示方向信息
// mapView.userTrackingMode = .follow
// 獲取用戶位置顯示方向信息
mapView.userTrackingMode = .followWithHeading
}
mapView.userTrackingMode = .follow的效果圖:
mapView.userTrackingMode = .followWithHeading的效果圖:
設(shè)置用戶大頭針信息
沒有實(shí)現(xiàn)功能前的效果圖:
實(shí)現(xiàn)功能后的效果圖:
代碼實(shí)現(xiàn):
@IBOutlet weak var mapView: MKMapView!
var manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// 請(qǐng)求用戶授權(quán)
manager.requestWhenInUseAuthorization()
// 設(shè)置用戶追蹤模式
mapView.userTrackingMode = .follow
// mapView.userTrackingMode = .followWithHeading
mapView.delegate = self
}
/// 更新用戶位置完成時(shí)候調(diào)用的MKMapView代理方法
///
/// - Parameters:
/// - mapView: MKMapView
/// - userLocation: 用戶位置屬性
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
let geocoder = CLGeocoder()
// 反地理編碼
geocoder.reverseGeocodeLocation(userLocation.location!) { (placemarks: [CLPlacemark]?, error: Error?) in
// 設(shè)置用戶位置的標(biāo)題是城市
userLocation.title = placemarks?.last?.locality
// 設(shè)置用戶位置的子標(biāo)題是具體位置
userLocation.subtitle = placemarks?.last?.name
}
}
MKUserLocation這個(gè)類指的是地圖上顯示用戶當(dāng)前位置的圓點(diǎn)息拜。設(shè)置title和subtitle屬性就可以設(shè)置用戶位置信息。
這個(gè)類遵守了MKAnnotation協(xié)議,實(shí)現(xiàn)了title和subtitle屬性少欺。
MKAnnotation協(xié)議
public protocol MKAnnotation : NSObjectProtocol {
public var coordinate: CLLocationCoordinate2D { get }. // 經(jīng)緯度類
optional public var title: String? { get } // 標(biāo)題
optional public var subtitle: String? { get } // 子標(biāo)題
}
總結(jié):
設(shè)置用戶位置知識(shí)點(diǎn)只需要兩步
- 設(shè)置代理喳瓣,實(shí)現(xiàn)mapView完成用戶位置更新方法。因?yàn)閷?shí)現(xiàn)這個(gè)代理方法才能獲取MKUserLocation對(duì)象
- 設(shè)置MKUserLocation這個(gè)類的title和subtitle屬性
以上代碼設(shè)置用戶位置信息知識(shí)點(diǎn)涉及到地理編碼赞别,如果不熟悉畏陕,可以參考筆者之前的iOS地理編碼的簡(jiǎn)單實(shí)現(xiàn)
這篇文章就寫到這里,希望對(duì)你們有幫助氯庆。