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)系作者砰嘁。
- 文/潘曉璐 我一進(jìn)店門盾碗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人舀瓢,你說我怎么就攤上這事廷雅。” “怎么了氢伟?”我有些...
- 文/不壞的土叔 我叫張陵榜轿,是天一觀的道長。 經(jīng)常有香客問我朵锣,道長谬盐,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任诚些,我火速辦了婚禮飞傀,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘诬烹。我一直安慰自己砸烦,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布绞吁。 她就那樣靜靜地躺著幢痘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪家破。 梳的紋絲不亂的頭發(fā)上颜说,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼拟蜻!你這毒婦竟也來了绎签?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布璧尸,位于F島的核電站,受9級特大地震影響熬拒,放射性物質(zhì)發(fā)生泄漏爷光。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一澎粟、第九天 我趴在偏房一處隱蔽的房頂上張望蛀序。 院中可真熱鬧,春花似錦徐裸、人聲如沸啸盏。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽粉怕。三九已至抒巢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間稚晚,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 本文主要介紹手機定位方式苛吱、百度地圖SDK[https://lbsyun.baidu.com/index.php?t...
- 轉(zhuǎn)載娱局、引用請標(biāo)明出處http://www.reibang.com/p/29ccac3e1e42本文出自zhh_ha...
- 今天遇到個問題,客戶給了個 矢量地圖任斋,然后給了 左上角 & 右下角 的經(jīng)緯度,讓我實時定位废酷,并將自己的位置顯示在適...
- 前言 公司一款A(yù)pp使用到了地圖定位趴俘,可是從AppStore下載下來后寥闪,第一次安裝完后出現(xiàn)了黑屏的現(xiàn)象达舒。 控制臺打...