0

i have two points 1 & 2 and each point has its own callout but when i try to use the taponcallout function

func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation ) {

it is reflected on both points which end up referring me to the same method

how can i make this function differentiate between the two points??

here is my code sample


let point = MGLPointAnnotation()
let point1 = MGLPointAnnotation()



 func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {

        // Create point to represent where the symbol should be placed

        point.coordinate = CLLocationCoordinate2D(latitude: 26.319735961914062, longitude: 50.14814726335609)
        point1.coordinate = CLLocationCoordinate2D(latitude: 26.319735961914062, longitude: 50.14414726335609)

        point.title = "big-bossman"
        point.subtitle = "very scary"

        point1.title = "smol-bossman"
        point1.subtitle = "smol scary"


        // Create a data source to hold the point data
        let shapeSource = MGLShapeSource(identifier: "marker-source", shape: point, options: nil)
        let shapeSource1 = MGLShapeSource(identifier: "marker-source1", shape: point1, options: nil)

        // Create a style layer for the symbol
        let shapeLayer = MGLSymbolStyleLayer(identifier: "marker-style", source: shapeSource)
        let shapeLayer1 = MGLSymbolStyleLayer(identifier: "marker-style1", source: shapeSource1)

        // Add the image to the style's sprite
        if let image = UIImage(named: "enemyImage") {
            style.setImage(image, forName: "home-symbol")
        }

        // Tell the layer to use the image in the sprite
        shapeLayer.iconImageName = NSExpression(forConstantValue: "home-symbol")
        shapeLayer1.iconImageName = NSExpression(forConstantValue: "home-symbol")

        // Add the source and style layer to the map
        style.addSource(shapeSource)
        style.addLayer(shapeLayer)

        style.addSource(shapeSource1)
        style.addLayer(shapeLayer1)

        mapView.selectAnnotation(point, animated: true, completionHandler: nil)
        mapView.selectAnnotation(point1, animated: true, completionHandler: nil)


    }

    func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
        true
    }

    func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation ) {


            transition()

    }

For example what i want is that if i click on the callout of point i get to transition() but if i click on point1 i get to transition1()

MCapital
  • 1
  • 1
  • You are are given the tapped annotation in the `tapOnCalloutFor` method. Just test the `annotation` parameter against your points and take the appropriate action. – Magnas Mar 15 '20 at 20:49

1 Answers1

0

Unsure if you tried the above solution, but this accomplishes what you want. If you reference the annotation against your points within the tapOnCalloutFor method and then set the appropriate action, it should work fine. In the code below, I used the annotation and point titles:

       if annotation.title == point.title {
                //do something
       } else if annotation.title == point1.title {
                //do something else 
       }
invaderzizim
  • 311
  • 1
  • 5