LocationManager

open class LocationManager : NSObject, CLLocationManagerDelegate

位置服务

注意:Info.plist需要添加NSLocationWhenInUseUsageDescription项。 如果请求Always定位,还需添加NSLocationAlwaysUsageDescription项和NSLocationAlwaysAndWhenInUseUsageDescription项。 iOS11可通过showsBackgroundLocationIndicator配置是否显示后台定位指示器

  • 单例模式

    Declaration

    Swift

    public static let shared: LocationManager
  • 坐标转“纬度,经度"字符串

    Declaration

    Swift

    open class func locationString(_ coordinate: CLLocationCoordinate2D) -> String
  • “纬度,经度"字符串转坐标

    Declaration

    Swift

    open class func locationCoordinate(_ string: String) -> CLLocationCoordinate2D
  • 计算两个经纬度间的距离

    Declaration

    Swift

    open class func locationDistance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance
  • 经纬度反解析为地址

    Declaration

    Swift

    @discardableResult
    open class func reverseGeocode(_ coordinate: CLLocationCoordinate2D, locale: Locale? = nil, completionHandler: @escaping CLGeocodeCompletionHandler) -> CLGeocoder
  • 地址解析为经纬度

    Declaration

    Swift

    @discardableResult
    open class func geocode(_ address: String, region: CLRegion? = nil, locale: Locale? = nil, completionHandler: @escaping CLGeocodeCompletionHandler) -> CLGeocoder
  • 是否启用Always定位,默认NO,请求WhenInUse定位

    Declaration

    Swift

    open var alwaysLocation: Bool
  • 是否启用后台定位,默认NO。如果需要后台定位,设为YES即可

    Declaration

    Swift

    open var backgroundLocation: Bool
  • 是否启用方向监听,默认NO。如果设备不支持方向,则不能启用

    Declaration

    Swift

    open var headingEnabled: Bool { get set }
  • 是否发送通知,默认NO。如果需要通知,设为YES即可

    Declaration

    Swift

    open var notificationEnabled: Bool
  • 定位完成是否立即stop,默认NO。如果为YES,只会回调一次

    Declaration

    Swift

    open var stopWhenCompleted: Bool
  • 位置管理对象

    Declaration

    Swift

    open lazy var locationManager: CLLocationManager { get set }
  • 当前位置,中途定位失败时不会重置

    Declaration

    Swift

    open private(set) var location: CLLocation? { get }
  • 当前方向,headingEnabled启用后生效

    Declaration

    Swift

    open private(set) var heading: CLHeading? { get }
  • 当前错误,表示最近一次定位回调状态

    Declaration

    Swift

    open private(set) var error: Error? { get }
  • 定位改变block方式回调,可通过error判断是否定位成功

    Declaration

    Swift

    open var locationChanged: ((LocationManager) -> Void)?
  • 开始更新位置

    Declaration

    Swift

    open func startUpdateLocation()
  • 停止更新位置

    Declaration

    Swift

    open func stopUpdateLocation()

CLLocationManagerDelegate