我最近决定从Mapkit改为Mapbox。我在地图中实现了注记,但由于某种原因,单击注记时未显示注记标注。我很困惑,不确定为什么没有出现。希望有人能帮忙!相应的代码如下所示:
import UIKit
import Firebase
import Mapbox
class ViewController: UIViewController, SideBarDelegate, MGLMapViewDelegate {
@IBOutlet weak var mapView: MGLMapView!
//Filtering annotations for sidebar
func sideBarDidSelectButtonAtIndex(_ index: Int) {
mapView.removeAnnotations(mapView.annotations!)
for park in skateparks {
if index == 0 {
addAnnotation(park: park)
}
if index == 1 && park.type == .park {
addAnnotation(park: park)
}
if index == 2 && park.type == .street {
addAnnotation(park: park)
}
}
}
var sideBar: SideBar = SideBar()
var skateparks = [Skatepark]()
let locationsRef = FIRDatabase.database().reference(withPath: "locations")
override func viewDidLoad() {
super.viewDidLoad()
//Location
mapView.delegate = self
mapView.showsUserLocation = true
//Sidebar
sideBar = SideBar(sourceView: self.view, skateItems: ["All Skate Spots", "Skateparks", "Street Skating"])
sideBar.delegate = self
// Passing firebase annotation data
locationsRef.observe(.value, with: { snapshot in
self.skateparks.removeAll()
for item in snapshot.children {
guard let snapshot = item as? FIRDataSnapshot else { continue }
let newSkatepark = Skatepark(snapshot: snapshot)
self.skateparks.append(newSkatepark)
self.addAnnotation(park: newSkatepark)
}
})
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
view.sendSubview(toBack: mapView)
}
func addAnnotation(park: Skatepark) {
let point = MGLPointAnnotation()
point.coordinate = park.coordinate
point.title = park.name
point.subtitle = park.subtitle
mapView.addAnnotation(point)
mapView.selectAnnotation(point, animated: true)
}
}
func mapView(mapView: MGLMapView, viewForAnnotation annotation: MGLAnnotation) -> MGLAnnotationView? {
return nil
}
func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}
更新你的 SWIFT 3 函数:
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool { // Always try to show a callout when an annotation is tapped. return true }
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
}
您可能会发现此示例很有帮助(如果您还没有看过它(! 它演示如何使用 Mapbox 标注委托。