- 配置头文件:
import MAMapKit
import AMapFoundationKit
- 显示地图:
注意: 若初始化地图并添加到View上后并没有显示到视图上的话,并且出现
Error Domain=AMapFoundationErrorPrivacyShowUnknow Code=555570 “(null)” UserInfo={info=使用MAMapKit3D SDK 功能前请设置隐私权政策是否弹窗告知用户}
在@main 文件里面初始化
//MARK: 高德地图
func initMap() {
AMapServices.shared().enableHTTPS = true
AMapServices.shared().apiKey = GaodeAppkey
}
在主页实现下面这个方法 弹出选择定位权限允许弹出框
@StateObject private var mapViewModel = LocationViewModel()
.onAppear {
//触发 定位隐私协议弹窗
mapViewModel.agreeToPrivacyAndRequestLocation()
}
//这是一个判断用户是否开启定位权限的方法
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
//重点 这两个必须要加上
AMapLocationManager.updatePrivacyShow(.didShow, privacyInfo: .didContain)
switch status {
case.authorizedWhenInUse :
print("用户选择了仅在使用应用期间允许访问位置")
//重点 这两个必须要加上
AMapLocationManager.updatePrivacyAgree(.didAgree)
appStartUpdatingLocation()
case.authorizedAlways:
print("用户选择了始终允许访问位置")
AMapLocationManager.updatePrivacyAgree(.didAgree)
appStartUpdatingLocation()
case.denied:
print("用户拒绝了访问位置")
AMapLocationManager.updatePrivacyAgree(.notAgree)
case.restricted:
print("位置访问受到限制,可能是由于系统策略等原因")
AMapLocationManager.updatePrivacyAgree(.unknow)
default:
print("其他状态或尚未做出选择")
AMapLocationManager.updatePrivacyAgree(.unknow)
}
}
这里是一个SwiftUI的示例、需要包装一下地图。如果是Swift代码 ,就直接写在 viewDidLoad 里面就可以了
//MARK: 包装地图View
struct AMapView: UIViewRepresentable {
// 目标点的坐标(通过 @Binding 动态更新)
@Binding var targetCoordinate: CLLocationCoordinate2D
func updateUIView(_ uiView: MAMapView, context: Context) {
// 当 targetCoordinate 发生变化时,更新地图中心点
uiView.setCenter(targetCoordinate, animated: true)
// 更新目标点标记 改变 coordinate就会自动更新 SwiftUI
if let annotation = uiView.annotations.first as? MAPointAnnotation {
annotation.coordinate = targetCoordinate
} else {
let annotation = MAPointAnnotation()
annotation.coordinate = targetCoordinate
annotation.title = "目标点"
uiView.addAnnotation(annotation)
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
// 创建 MAMapView
func makeUIView(context: Context) -> MAMapView {
let mapView = MAMapView(frame: .zero)
mapView.delegate = context.coordinator // 设置代理
mapView.showsUserLocation = true // 显示用户位置
mapView.userTrackingMode = .follow // 跟随用户位置
mapView.setZoomLevel(17.5, animated: true)
// 添加目标点标记
let annotation = MAPointAnnotation()
annotation.coordinate = targetCoordinate
annotation.title = "目标点"
mapView.addAnnotation(annotation)
// 将地图中心移动到目标点
mapView.setCenter(targetCoordinate, animated: true)
return mapView
}
// Coordinator 类,实现 MAMapViewDelegate
class Coordinator: NSObject, MAMapViewDelegate {
var parent: AMapView
init(_ parent: AMapView) {
self.parent = parent
}
// 自定义标记图标
func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
if annotation.isKind(of: MAPointAnnotation.self) {
let annotationView = MAPinAnnotationView(annotation: annotation, reuseIdentifier: "annotationReuseIndetifier")
annotationView?.image = UIImage(named: "homepage_detail_location_end") // 自定义标记图标
return annotationView
}
return nil
}
}
}
- 在加载定位蓝点时一定要将下面的三个key都添加在Info中:
<key>Privacy - Location Always Usage Description</key>
<string>始终允许访问位置信息</string>
<key>Privacy - Location Usage Description</key>
<string>永不允许访问位置信息</string>
<key>Privacy - Location When In Use Usage Description</key>
<string>使用应用期间允许访问位置信息</string>
-
总结
这里是简单做了一个自定义标记point图标、有很多功能点击方法进去源文件里面可以看到很多的方法……有兴趣可以一一尝试