swift - 如何在 MapKit 中使用通用 Apple 地图图钉来定位位置,而不必使用我自己的图像作为图钉?
问题描述
在我的应用程序中,我在 FireBase 中存储了坐标,并在下面的代码中的地图视图上显示了这些坐标。用户的当前位置也被利用。我希望我存储在 FireBase 中的位置由通用 Apple Maps 引脚表示,但我不知道如何让应用程序工作,除非我使用自定义图像作为引脚。此外,地图上显示的用户的通用“蓝点”代表当前位置不会出现在我面前。在将位置存储在应用程序中而不是在 FireBase 中的不同项目中,通用图钉和蓝点会按照我的意愿出现。我怎样才能解决这个问题?
我尝试过更改代码的零碎部分,但我对 Swift 和整个编程世界还很陌生,找不到解决方案。
类视图控制器:UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBAction func mapSwitch(_ sender: UISwitch) {
if (sender.isOn == true) {
mapView.mapType = MKMapType.standard
}
else {
mapView.mapType = MKMapType.hybrid
}
}
var tappedAnnotation : MKAnnotation?
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let view = mapView.dequeueReusableAnnotationView(withIdentifier: "Annotation") {
view.annotation = annotation
view.image = UIImage(named: "pin")
return view
} else {
let view = MKAnnotationView(annotation: annotation, reuseIdentifier: "Annotation")
view.image = UIImage(named: "pin")
view.isEnabled = true
view.canShowCallout = true
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
return view
}
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
tappedAnnotation = view.annotation
performSegue(withIdentifier: "showAnnotationDetails", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showAnnotationDetails", let dest = segue.destination as? AnnotationDetails {
dest.annotation = tappedAnnotation
}
}
func createAnnotations ( _ annotations : [String:[String:Any]] ) {
mapView.removeAnnotations(mapView.annotations)
for (_,values) in annotations {
if let latDouble = values["latitude"] as? Double, let longDouble = values["longitude"] as? Double {
let lat = CLLocationDegrees( latDouble )
let long = CLLocationDegrees( longDouble )
let coord = CLLocationCoordinate2D(latitude: lat, longitude: long)
let annotation = MKPointAnnotation()
annotation.coordinate = coord
annotation.title = values["name"] as? String ?? ""
annotation.subtitle = values["info"] as? String ?? ""
mapView.addAnnotation(annotation)
}
}
}
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
mapView.delegate = self
mapView.mapType = MKMapType.standard
let ref = Database.database().reference()
ref.child("annotations").observe(.value) { snapshot in
print(snapshot)
if let annotations = snapshot.value as? [String:[String:Any]] {
self.createAnnotations(annotations)
} else {
print("Data received not formatted as expected")
}
}
}
解决方案
- 使用
MKPinAnnotationView
代替MKAnnotationView
- 设置
mapView.showsUserLocation = true
为显示用户位置(蓝点)
推荐阅读
- c# - SQLite 数据库未关闭
- c# - Visual Studio 2017 15.8版中如何去除水平白线?
- gooddata - Gooddata 报告按商店显示天气
- spring-boot - 如何识别客户端是否意外终止对我的 SpringBoot API 的调用(在能够返回响应之前)
- php - 删除特定类 domdocument
- git - 在 ubuntu 上安装 gitlab 后如何发出提交
- python - Pandas 使用自定义体积加权聚合进行重采样
- spring-integration - Spring Integration dsl - HTTP 入站网关使用 multipart/form-data
- c# - C# MVVM 如何将命令添加到 TextBlock
- ruby - 尝试使用 wget 或 curl 下载文件时如何避免重定向?