Detect tap on GMSPolyline in Swift?

匿名 (未验证) 提交于 2019-12-03 08:39:56

问题:

I'm struggling with detecting a tap on a GMSPolyline drawn on my Google map, it works just fine with GMSpolygones, but the same approach doesn't seem to work with polyline. My current approach, which works for polygones, is:

if (GMSGeometryContainsLocation(coordinate, polygon.path!, false)) {     ... } 

Any suggestions how to detect taps on a polyline? Or just close to it?

回答1:

According to their API documentation, GMSPolyline inherits from GMSOverlay which means a GMSPolyline has the property tappable. So you'd want something like this

let polyLine: GMSPolyline = GMSPolyline(path: newPath) polyLine.isTappable = true polyline.map = yourGoogleMap 

Then your GMSMapViewDelegate should notify you of the tap anywhere within the GMSPolyline layer with this function

func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {   print("User Tapped Layer: \(overlay)") } 


回答2:

You can use isTappable property of GMSPolyline.

isTappable

If this overlay should cause tap notifications.

polyline.isTappable = true

GMSPolyline inherits from GMSOverlay. So to detect tap on overlays GMSMapViewDelegate provides a delegate method:

  • mapView:didTapOverlay: Called after an overlay has been tapped.

Whenever the polyline is tapped, the GMSMapViewDelegate method didTapOverlay is called

func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {         //Write your code here     } 

Also, this method can be used for GMSPolygon since it also inherits from GMSOverlay.

For further information refer https://developers.google.com/maps/documentation/ios-sdk/reference/protocol_g_m_s_map_view_delegate-p.html#a3a2bf2ff4481528f931183cb364c0f4b



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