我最近决定从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
}
答案 0 :(得分:1)
更新您的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? {
}
答案 1 :(得分:0)
您可能会发现this example有帮助(如果您还没有看到它)! 它显示了如何使用Mapbox标注委托。