1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- //
- // SBTLocation.swift
- // SolarBT
- //
- // Created by weclouds on 2019/9/23.
- // Copyright © 2019 weclouds. All rights reserved.
- //
- import UIKit
- import CoreLocation
- class SBTLocation: NSObject,CLLocationManagerDelegate {
- var callback : ((String,String)->Void)?
- static let share = SBTLocation()
- var locationManager = CLLocationManager()
-
- override init() {
- super.init()
-
- }
-
- func startLocation() {
- log.debug("")
-
- locationManager.delegate = self
- locationManager.requestAlwaysAuthorization()
- locationManager.desiredAccuracy = kCLLocationAccuracyBest//定位最佳
- locationManager.distanceFilter = 500.0//更新距离
- locationManager.requestWhenInUseAuthorization()
- if (CLLocationManager.locationServicesEnabled()){
- locationManager.startUpdatingLocation()
- print("定位开始")
- }
-
-
-
- }
-
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- //取得Locations数组的最后一个
- // log.debug
- let location:CLLocation = locations.last ?? CLLocation()
- //判断是否为空
- let lat = "\(location.coordinate.latitude)"
- let long = "\(location.coordinate.longitude)"
- print("经纬度----\(lat),\(long)")
- callback!(lat,long)
- //停止定位
- locationManager.stopUpdatingLocation()
- }
-
- //出现错误
- func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) {
- print("error - \(error)")
- }
- func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
- locationManager.stopUpdatingLocation()
- }
- }
|