MrD*_*ane 0 xcode mapkit mkannotation ios swift
我想根据特定列表以绿色显示注释。我该怎么做?这是我的代码:
for i in closestpharmacyname{
var docref2 = db.collection("pharmacies").document(i)
print("Pharmacy: ", i)
docref2.getDocument(source: .cache) { (document, error) in
if var document = document {
var pharmacylatitude = document.get("latitude") as! Double
var pharmacylongitude = document.get("longitude") as! Double
print(pharmacylatitude, pharmacylongitude)
var pharmacyannotation = MKPointAnnotation()
pharmacyannotation.coordinate = CLLocationCoordinate2D(latitude: pharmacylatitude, longitude: pharmacylongitude)
pharmacyannotation.title = i
self.MapView.addAnnotation(pharmacyannotation)
}else{
print("Document does not exist")
}
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,除了注释是标准的红色。
尝试使用 MKMarkerAnnotationView 代替,这样您就可以自定义颜色。据我所知,不可能更改 MKPointAnnotation 的颜色。MKMarkerAnnotationView 看起来也一样。
更具体地说,您需要添加此函数,根据我的理解,该函数采用 MKPointAnnotation 并返回 MKMarkerAnnotationView,您可以自定义每个注释,这意味着您可以使用 switch 语句有条件地为每个注释赋予不同的颜色。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "something")
annotationView.markerTintColor = .green //custom colors also work, additionally to these default ones
return annotationView
}
Run Code Online (Sandbox Code Playgroud)
为了更好地理解自定义注释,包括图片中的自定义符号而不是默认符号,请查看此链接: https://medium.com/better-programming/how-to-customize-mapkit-annotations-baad32487a7
| 归档时间: |
|
| 查看次数: |
2680 次 |
| 最近记录: |