Mapbox Annotation Callout

837 views Asked by At

I have recently decided to change from Mapkit to Mapbox. I have my annotations implemented within my map but for some reason my annotation callouts are not appearing when I click on the annotation. I am quite confused and unsure as to why this is not appearing. Hopefully somebody can help! The appropriate code is shown below:

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
}
2

There are 2 answers

3
Tushar Sharma On

UPDATE YOUR SWIFT 3 FUNCTIONS-:

  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
jmkiley On

You may find this example helpful (if you have not already seen it)! It shows how to use the Mapbox callout delegate.