前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >swift 定位封装一句话使用

swift 定位封装一句话使用

作者头像
星宇大前端
发布2019-01-15 15:41:08
1.3K0
发布2019-01-15 15:41: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))")
            }
        }
    }
    
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年05月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档