使用定位功能需要需要接入高德地图定位Api:
pod 'AMapLocation'
配置Info
在info中新建一个名为Privacy - Location Temporary Usage Description Dictionary的字典,然后在这个字典下新建Privacy - Location When In Use Usage Description、Privacy - Location Always and When In Use Usage Description两个字段,内容就是权限申请时的描述。
初始化定位管理类
import AMapLocationKit
var locationManage:AMapLocationManager!
实现定位代理AMapLocationManagerDelegate
设置代理和开始定位
//初始化定位
func initLocation(){
locationManage=AMapLocationManager()
locationManage.delegate=self
Permission.location(access: .always).request {
//连续定位是否返回逆地理信息
self.locationManage.locatingWithReGeocode=true
self.locationManage.startUpdatingLocation()
}
}
Permission是使用的一个权限申请框架PermissionsKit,这里就不细讲了,给出链接:PermissionsKit
实现代理方法
需要实现两个代理方法:
//定位回调
func amapLocationManager(_ manager: AMapLocationManager!, didUpdate location: CLLocation!) {
let infoString = String(format: "回调时间:%@\n经 度:%.6f\n纬 度:%.6f\n精 度:%.3f米\n海 拔:%.3f米\n速 度:%.3f\n角 度:%.3f\n", location.timestamp.description, location.coordinate.longitude, location.coordinate.latitude, location.horizontalAccuracy, location.altitude, location.speed, location.course)
print(infoString)
}
//不知道干嘛的,SDK要求的
func amapLocationManager(_ manager: AMapLocationManager!, doRequireLocationAuth locationManager: CLLocationManager!) {
print("location")
}