обработка различных выносок MGLPointAnnotation - PullRequest
0 голосов
/ 15 марта 2020

У меня есть две точки 1 и 2, и у каждой точки есть свой выноски, но когда я пытаюсь использовать функцию taponcallout

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

, это отражается на обеих точках, которые заканчиваются ссылаясь на один и тот же метод

как я могу заставить эту функцию различать две точки ??

вот мой пример кода


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()

    }

Например, что я хочу в том, что, если я нажимаю на выноску из точки , я получаю переход (), но если я нажимаю point1 , я получаю к transition1 ()

1 Ответ

0 голосов
/ 07 мая 2020

Не уверен, что вы попробовали вышеуказанное решение, но это выполняет то, что вы хотите. Если вы ссылаетесь на аннотацию относительно ваших точек в методе tapOnCalloutFor, а затем устанавливаете соответствующее действие, оно должно работать нормально. В приведенном ниже коде я использовал аннотации и точечные заголовки:

       if annotation.title == point.title {
                //do something
       } else if annotation.title == point1.title {
                //do something else 
       }
...