首先是真機(jī)調(diào)試,模擬器不行,其次請?zhí)砑觾蓚€位置獲取權(quán)限Privacy - Location Always Usage Description和Privacy - Location When In Use Usage Description
添加兩個組件MapKit和CoreLocation
import?UIKit
import?MapKit
import?CoreLocation
class?FirstViewController2:UIViewController?,CLLocationManagerDelegate{
?var?locationManager :CLLocationManager!
?var?currLocation :CLLocation!
?lazyvar?mapView:MKMapView?= {
?let?mapView =MKMapView(frame:UIScreen.main.bounds)
?//用戶位置追蹤(用戶位置追蹤用于標(biāo)記用戶當(dāng)前位置,此時會調(diào)用定位服務(wù))
? ? ? ? mapView.userTrackingMode?= .followWithHeading
?//地圖的顯示風(fēng)格贯涎,此處設(shè)置使用標(biāo)準(zhǔn)地圖
? ? ? ? mapView.mapType?= .standard
?//地圖是否可滾動,默認(rèn)為true
? ? ? ? mapView.isScrollEnabled?=true
?//地圖是否縮放,默認(rèn)為true
? ? ? ? mapView.isZoomEnabled?=true
?//是否顯示用戶當(dāng)前位置?ios8之后才有荧止,默認(rèn)為false
? ? ? ? mapView.showsUserLocation?=true
?//為MKMapView設(shè)置delegate
?//mapView.delegate = self
?if#available(iOS9.0, *) {
? ? ? ? ? ? mapView.showsCompass?=true//顯示指南針
? ? ? ? ? ? mapView.showsScale?=true//顯示比例尺
? ? ? ? ? ? mapView.showsTraffic?=true//顯示交通
? ? ? ? }
? ? ? ? mapView.showsBuildings?=true
? ? ? ? mapView.showsUserLocation?=true
?return?mapView
? ? }()
?overridefunc?viewDidLoad() {
?view.backgroundColor?=UIColor.white
?//初始化位置管理器
?locationManager?=CLLocationManager()
?locationManager.delegate?=self
?//設(shè)備使用電池供電時最高的精度
?locationManager.desiredAccuracy?=kCLLocationAccuracyBest
?//精確到1000米,距離過濾器,定義了設(shè)備移動后獲得位置信息的最小距離
?locationManager.distanceFilter?=kCLLocationAccuracyKilometer
?if#available(iOS8.0, *) {
?//如果是IOS8及以上版本需調(diào)用這個方法
?locationManager.requestAlwaysAuthorization()
? ? ? ? ? ?//使用應(yīng)用程序期間允許訪問位置數(shù)據(jù)
?locationManager.requestWhenInUseAuthorization();
?//啟動定位
?locationManager.startUpdatingLocation()
? ? ? ? }
?view.addSubview(self.mapView)
? ? }
?//FIXME: CoreLocationManagerDelegate中獲取到位置信息的處理函數(shù)
?func?locationManager(_?manager:CLLocationManager, didUpdateLocations locations: [CLLocation]) {
?let?location:CLLocation?= locations[locations.count-1]asCLLocation
?currLocation?= location
?if?(location.horizontalAccuracy?>0) {
?self.locationManager.stopUpdatingLocation()
?//print("wgs84坐標(biāo)系?緯度: \(location.coordinate.latitude)經(jīng)度: \(location.coordinate.longitude)")
?self.locationManager.stopUpdatingLocation()
?//print("結(jié)束定位")
? ? ? ? }
?//坐標(biāo)轉(zhuǎn)換成地址
?let?geocoder =CLGeocoder()
? ? ? ? geocoder.reverseGeocodeLocation(currLocation) { (placemark, error) ->?Voidin
?if(error ==nil)//成功
? ? ? ? ? ? {
?let?array = placemark!asNSArray
?let?mark = array.firstObjectas!CLPlacemark
?let?FormattedAddressLines:NSString?= ((mark.addressDictionary!asNSDictionary).value(forKey:"FormattedAddressLines")asAnyObject).firstObjectas!NSString
?print(FormattedAddressLines)
}?else?{
?print(error!)//獲取位置信息失敗
? ? ? ? ? ? }
? ? ? ? }
? ? }
?//FIXME:?獲取位置信息失敗
?func? locationManager(_?manager:CLLocationManager, didFailWithError error:Error) {
?print(error)
? ? }
?overridefunc?viewWillAppear(_?animated:Bool) {
?super.viewWillAppear(animated)
?//創(chuàng)建左邊按鈕
?let?leftBtn =UIBarButtonItem(title:"<?返回", style: .plain, target:?self, action:#selector(chartViewController.btnBack(_:)))
?self.title?="定位地圖"
?self.navigationItem.leftBarButtonItem?= leftBtn
?self.navigationItem.leftBarButtonItem?.tintColor?=?UIColor(colorLiteralRed:255/255, green:255/255, blue:255/255, alpha:1)
?self.navigationController?.navigationBar.barTintColor?=
?UIColor(red:0/255, green:166/255, blue:124/255, alpha:1)
?self.navigationController?.navigationBar.titleTextAttributes?=
? ? ? ? ? ? [NSForegroundColorAttributeName:?UIColor(colorLiteralRed:255/255, green:255/255, blue:255/255, alpha:1)]
?//創(chuàng)建左邊保存按鈕
?let?item=UIBarButtonItem(title:"保存", style:UIBarButtonItemStyle.plain, target:self, action:#selector(threeViewController.tapped2))
?self.navigationItem.rightBarButtonItem?= item
?self.navigationItem.rightBarButtonItem?.tintColor?=?UIColor(colorLiteralRed:255/255, green:255/255, blue:255/255, alpha:1)
? ? }
?func?btnBack(_?sender:UIButton) {
?self.presentingViewController!.dismiss(animated:true, completion:nil)
? ? }
}