swift 定位封裝一句話使用

ZY_FlyWay發表於2018-05-08

swift 定位的封裝,使用一句話方便簡單。

閉包返回資訊:定位Location+反編譯地址資訊+error

git地址:https://github.com/RainManGO/LocationManager

使用程式碼:

 LocationManager.shareManager.creatLocationManager().startLocation { (location, adress, error) in
            print("經度 \(location?.coordinate.longitude ?? 0.0)")
            print("緯度 \(location?.coordinate.latitude ?? 0.0)")
            print("地址\(adress ?? "")")
            print("error\(error ?? "沒有錯誤")")
        }


封裝程式碼:


//
//  LocationManager.swift
//  ZYLocationManager
//
//  Created by Nvr on 2018/5/8.
//  Copyright © 2018年 ZY. All rights reserved.
//

import UIKit
import CoreLocation

class LocationManager: NSObject {
    
    typealias locationCallBack = (_ curLocation:CLLocation?,_ curAddress:String?,_ errorReason:String?)->()
    
    //MARK:-屬性
    
    ///單例,唯一呼叫方法
    static let shareManager:LocationManager =  LocationManager()
    
    
    private override init() {
        
    }
    
    var manager:CLLocationManager?
    
    //當前座標
    var curLocation: CLLocation?
    //當前選中位置的座標
    var curAddressCoordinate: CLLocationCoordinate2D?
    //當前位置地址
    var curAddress: String?
    
    
    //回撥閉包
    var  callBack:locationCallBack?
    
    func creatLocationManager() -> LocationManager{
        manager = CLLocationManager()
        //設定定位服務管理器代理
        manager?.delegate = self
        //設定定位模式
        manager?.desiredAccuracy = kCLLocationAccuracyBest
        //更新距離
        manager?.distanceFilter = 100
        //傳送授權申請
        manager?.requestWhenInUseAuthorization()
        
        return self
    }
    
    //更新位置
    open func startLocation(resultBack:@escaping locationCallBack){
        
        self.callBack = resultBack
        
        if CLLocationManager.locationServicesEnabled(){
            //允許使用定位服務的話,開啟定位服務更新
            manager?.startUpdatingLocation()
            print("定位開始")
        }
    }
    
}


extension LocationManager:CLLocationManagerDelegate {
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        //獲取最新的座標
        curLocation = locations.last!
        //停止定位
        if locations.count > 0{
            manager.stopUpdatingLocation()
            LonLatToCity()
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        callBack!(nil,nil,"定位失敗===\(error)")
    }
    
    ///經緯度逆編
    func LonLatToCity() {
        let geocoder: CLGeocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(self.curLocation!) { (placemark, error) -> Void in
            if(error == nil){
                let firstPlaceMark = placemark!.first
                
                self.curAddress = ""
                //省
                if let administrativeArea = firstPlaceMark?.administrativeArea {
                    self.curAddress?.append(administrativeArea)
                }
                //自治區
                if let subAdministrativeArea = firstPlaceMark?.subAdministrativeArea {
                    self.curAddress?.append(subAdministrativeArea)
                }
                //市
                if let locality = firstPlaceMark?.locality {
                    self.curAddress?.append(locality)
                }
                //區
                if let subLocality = firstPlaceMark?.subLocality {
                    self.curAddress?.append(subLocality)
                }
                //地名
                if let name = firstPlaceMark?.name {
                    self.curAddress?.append(name)
                }
                
                self.callBack!(self.curLocation,self.curAddress,nil)
                
            }else{
                self.callBack!(nil,nil,"\(String(describing: error))")
            }
        }
    }
    
}


相關文章