import UIKit
import CoreLocation
class ViewController: UIViewController {
lazy var locationM: CLLocationManager = {
let locationM = CLLocationManager()
locationM.delegate = self
if #available(iOS 8.0, *) {
locationM.requestAlwaysAuthorization()
}
locationM.desiredAccuracy = kCLLocationAccuracyBest
return locationM
}()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
// 定位: 标准定位服务 (gps/wifi/蓝牙/基站)
// locationM.startUpdatingLocation()
// 显著位置变化的服务(基站进行定位, 电话模块)
// locationM.startMonitoringSignificantLocationChanges()
if #available(iOS 9.0, *) {
// 不能与 startUpdatingLocation同时使用
// 必须实现代理的定位失败的方法
locationM.requestLocation()
}
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("已经获取到位置信息")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("定位失败")
// manager.startUpdatingLocation()
}
}
swfit地图定位 (二)
最新推荐文章于 2024-09-22 22:22:49 发布