[英]Mapbox Annotation Callout
我最近決定將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標注委托。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.