2017-03-28 171 views
0

我最近決定從Mapkit更改爲Mapbox。我在我的地圖中實現了註釋,但由於某些原因,當我單擊註釋時,我的註釋標註沒有出現。我很困惑,不確定爲什麼這不出現。希望有人能幫助!相應的代碼如下所示: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

您是否實施了註釋代表? –

+0

嘿圖沙爾,我有! –

+0

您可以檢查一次 - :https://www.mapbox.com/help/first-steps-ios-sdk/ –

回答

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? { 

} 
+0

我已經更新了功能,但標註仍然不可見! –

+0

你可以把一個斷點,並檢查它是否被調用。 –

+0

@John Smith我再次更新了函數。 –

1

你會發現this example有用的(如果你還沒有已經看過了)! 它顯示瞭如何使用Mapbox標註代理。