地圖定位

import UIKit

@objc public protocol ADLocationProtocol {
    //得到經(jīng)緯度
    @objc optional func locationManager(latitude: Double, longitude: Double)

    //獲取城市信息
    @objc optional func locationCityInfo(cityName: String)
    
    /// 定位城市失敗
    @objc optional func locationCityFail()

}

class ADLocation: NSObject {

    public weak var delegate: ADLocationProtocol?
    
    public static let shared = ADLocation.init()
        
    private var locationManager: CLLocationManager?
    
    
    public func didUpdateLocation(_ vc: UIViewController){
    
        self.delegate = vc as? ADLocationProtocol
       
        self.requestLocationService()
        
    }
    
    //初始化定位
    private func requestLocationService(){
        if self.locationManager == nil {
            self.locationManager = CLLocationManager()
            self.locationManager?.delegate = self
        }
        
        
        self.locationManager?.requestWhenInUseAuthorization()
//        self.locationManager?.startUpdatingLocation()
        
        if CLLocationManager.authorizationStatus() == .denied {
            //定位服務(wù)未開啟
            self.alert()
        }else if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.notDetermined {
            //未知錯誤
            locationManager?.requestWhenInUseAuthorization()
        }else if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways {
            locationManager?.desiredAccuracy = kCLLocationAccuracyBest
            let distance: CLLocationDistance = 100
            locationManager?.distanceFilter = distance
            locationManager?.startUpdatingLocation()
        }
    }
    
    //獲取定位代理返回狀態(tài)進(jìn)行處理
    private func reportLocationServicesAuthorizationStatus(status: CLAuthorizationStatus) {
        if status == .notDetermined {
            //未知鲤嫡,繼續(xù)請求授權(quán)
            requestLocationService()
        }else if(status == .restricted || status == .denied){
            //受限制,提示授權(quán)
            self.alert()
            
            guard let delegate = self.delegate else { return }
            delegate.locationCityFail?()
        }else {
            //重新請求
            requestLocationService()
        }
    }
    
    private func alert(){
        ADAlert.alertTitle(title: "定位權(quán)限未開啟", content: "請在設(shè)置中開啟appName定位權(quán)限", textAlignment: .center, leftTitle: "暫不", rightTitle: "去設(shè)置") { (result) in
            if result {
                let url = NSURL.init(string: UIApplication.openSettingsURLString)!
                if UIApplication.shared.canOpenURL(url as URL) {
                    UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
                }
            }
        }
    }
}


extension ADLocation: CLLocationManagerDelegate{
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        //停止定位
        self.locationManager?.stopUpdatingLocation()
        
        let location = locations.last ?? CLLocation.init()
        let coordinate = location.coordinate
        
        let latitude: Double = coordinate.latitude
        let longitude: Double = coordinate.longitude
        
        delegate?.locationManager?(latitude: latitude, longitude: longitude)
        
        let geocoder = CLGeocoder.init()
        geocoder.reverseGeocodeLocation(location) { (placemarkList, error) in
            guard let placemarkList = placemarkList else { return }
            if placemarkList.count > 0 {
                guard let placemark = placemarkList.first else { return  }
                var city = placemark.locality
                if city == nil {
                    city = placemark.administrativeArea
                }
                debugPrint("city \(city)")
                
                guard let delegate = self.delegate else { return }
                delegate.locationCityInfo?(cityName: city ?? "")
            }
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        debugPrint("權(quán)限改變")
        reportLocationServicesAuthorizationStatus(status: status)
    }
    
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        debugPrint("定位失敗")
        guard let delegate = self.delegate else { return }
        delegate.locationCityFail?()
        
        self.locationManager?.stopUpdatingLocation()
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
禁止轉(zhuǎn)載沙郭,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者砰嘁。
  • 序言:七十年代末歪赢,一起剝皮案震驚了整個濱河市缀程,隨后出現(xiàn)的幾起案子缺亮,更是在濱河造成了極大的恐慌蜗字,老刑警劉巖盗迟,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件涵叮,死亡現(xiàn)場離奇詭異惭蹂,居然都是意外死亡,警方通過查閱死者的電腦和手機割粮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門盾碗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人舀瓢,你說我怎么就攤上這事廷雅。” “怎么了氢伟?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵榜轿,是天一觀的道長。 經(jīng)常有香客問我朵锣,道長谬盐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任诚些,我火速辦了婚禮飞傀,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘诬烹。我一直安慰自己砸烦,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布绞吁。 她就那樣靜靜地躺著幢痘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪家破。 梳的紋絲不亂的頭發(fā)上颜说,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天,我揣著相機與錄音汰聋,去河邊找鬼门粪。 笑死,一個胖子當(dāng)著我的面吹牛烹困,可吹牛的內(nèi)容都是我干的玄妈。 我是一名探鬼主播,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼拟蜻!你這毒婦竟也來了绎签?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤酝锅,失蹤者是張志新(化名)和其女友劉穎辜御,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體屈张,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡擒权,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了阁谆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片碳抄。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖场绿,靈堂內(nèi)的尸體忽然破棺而出剖效,到底是詐尸還是另有隱情,我是刑警寧澤焰盗,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布璧尸,位于F島的核電站,受9級特大地震影響熬拒,放射性物質(zhì)發(fā)生泄漏爷光。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一澎粟、第九天 我趴在偏房一處隱蔽的房頂上張望蛀序。 院中可真熱鬧,春花似錦徐裸、人聲如沸啸盏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽粉怕。三九已至抒巢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間稚晚,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工鸳劳, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留也搓,地道東北人。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓幔摸,卻偏偏與公主長得像颤练,于是被迫代替她去往敵國和親嗦玖。 傳聞我的和親對象是個殘疾皇子患雇,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容