Is it possible to create a custom popup for a MGLPolyline?

安稳与你 提交于 2019-12-13 04:25:06

问题


So I have been looking for a way to make an MGLPolyline tapable. One way I have thought about for doing this would be to use that popup thing that MapBox has added to make it show something, but make the popup invisible that way I can still do something (call function) when a certain line is taped.

Currently when you add polyline.title then when you tap the bellow function runs, but withough this or setting it to nothing ("") then it does not run

        func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
        // Always allow callouts to popup when annotations are tapped.
        print("ok ran?")
        return true
    }

However I have been unable to find a way to make the popup invisible or not show up so that I can just run a function when its taped. Is there a way to do this? Or a way in general to achive what I want to do in a scalable way?

Update:

        func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
        // Always allow callouts to popup when annotations are tapped.
        print("ok ran?")
        return false
    }

By changing the above return to false it seemed to recognize the touch without showing anything. Is this the best way to achieve what I want? It does not seem great given that sometimes when I touch it does not recognize.


回答1:


When you tap on the MGLPolyline you are selecting it. Even when you have returned false from annotationCanShowCallout: the polyline is selected after a tap even though there is probably no visible cue. This is why some of your taps have no visible action. These taps are deselecting the polyline (again invisibly).

What you can do is return false from annotationCanShowCallout: (assuming you don't want ANY annotations to have a callout) and use another delegate method to achieve your desired goal.

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

func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) {
    print("Tapped")
    mapView.deselectAnnotation(annotation, animated: false)
}

By immediately deselecting the annotation you can register each tap as a selection, eliminating the missed ones.



来源:https://stackoverflow.com/questions/54612568/is-it-possible-to-create-a-custom-popup-for-a-mglpolyline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!