简体   繁体   English

检测Swift中的GMSPolyline?

[英]Detect tap on GMSPolyline in Swift?

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. 我正在努力检测在我的谷歌地图上绘制的GMSPolyline上的水龙头,它与GMSpolygones一起工作得很好,但同样的方法似乎不适用于折线。 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? 或者只是靠近它?

According to their API documentation , GMSPolyline inherits from GMSOverlay which means a GMSPolyline has the property tappable . 根据他们的API 文档GMSPolyline继承自GMSOverlay ,这意味着GMSPolyline具有tappable属性。 So you'd want something like this 所以你想要这样的东西

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

Then your GMSMapViewDelegate should notify you of the tap anywhere within the GMSPolyline layer with this function 然后,您的GMSMapViewDelegate应该通过此功能通知您GMSPolyline图层中的任何位置

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

You can use isTappable property of GMSPolyline. 您可以使用isTappable属性。

isTappable isTappable

If this overlay should cause tap notifications. 如果此叠加层应该导致点按通知。

polyline.isTappable = true polyline.isTappable = true

GMSPolyline inherits from GMSOverlay . GMSPolyline继承自GMSOverlay So to detect tap on overlays GMSMapViewDelegate provides a delegate method: 因此,为了检测覆盖上的点击, GMSMapViewDelegate提供了一个委托方法:

  • mapView:didTapOverlay: Called after an overlay has been tapped. mapView:didTapOverlay:在点击叠加层后调用。

Whenever the polyline is tapped, the GMSMapViewDelegate method didTapOverlay is called 每当点击折线时, didTapOverlay调用GMSMapViewDelegate方法didTapOverlay

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 . 此外,此方法可用于GMSPolygon因为它也继承自GMSOverlay

For further information refer https://developers.google.com/maps/documentation/ios-sdk/reference/protocol_g_m_s_map_view_delegate-p.html#a3a2bf2ff4481528f931183cb364c0f4b 有关详细信息,请参阅https://developers.google.com/maps/documentation/ios-sdk/reference/protocol_g_m_s_map_view_delegate-p.html#a3a2bf2ff4481528f931183cb364c0f4b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM