ios - 子类化 GMSMarker 和 GMSMapViewDelegate didTapMarker

标签 ios swift google-maps

我正在使用这个简单的 MyMarker

class MyMarker: GMSMarker {
    var id: UInt32 = 0
}

这样我的标记也可以包含一个额外的数字标签。当用户点击我的标记时,我调用 segue 打开一个新场景,其中的内容是动态的,并且根据 MyMarkerid< 绘制。我想做这样的事情:

  func mapView(mapView: GMSMapView, didTapMarker marker: MyMarker) -> Bool {
        some_global_variable = marker.id;
        performSegueWithIdentifier("segue", sender: nil)
        return true
    }

问题当然是 GMSMapViewDelegate 期望标记的类型为 GMSMarker

我怎样才能实现我想要的行为?

最佳答案

您需要在委托(delegate)方法中将 GMSMarker 类型转换为您的自定义标记,不要更改 GMSMapViewDelegate 方法的签名。

func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
    if let myMarker = marker as? MyMarker {
         some_global_variable = myMarker.id
         performSegueWithIdentifier("segue", sender: nil)
    }
    return true
}

关于ios - 子类化 GMSMarker 和 GMSMapViewDelegate didTapMarker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42903027/

相关文章:

javascript - Google maps API V3 - 无法对 AutocompleteService 预测进行地理编码

ios - 您必须指定 |clientID|对于 |GIDSignIn|尝试使用 Google 登录时出错

python-2.7 - 使用python在谷歌地图上绘制点/圆

ios - 如何更改我的 UICollectionViewCell 中的属性?

ios - 在 iOS 中创建不适合整个屏幕的 Google map

arrays - 如何在 Swift 中使用 for 循环遍历元组数组

Swift NSCalendarUnit setValue forComponent 不工作

google-maps - Google Maps缓存kml文​​件多长时间?

android - 在移动设备上禁用 css 动画

ios - 在带有动画的 UITableView 中的 2 个 UITableviewCells 之间添加 UITableViewCells