我添加了一个标记
var marker: GMSMarker?
let position = CLLocationCoordinate2D(latitude: 38.890106, longitude: -77.007362)
marker = GMSMarker(position: position)
var data = Dictionary<String, Any>()
data["type"] = "Mark1" as String
marker?.userData = data
marker?.map = self.map
现在在func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
中,我想获取`[“ type”]值,为此,当我这样做时:
if marker.userData["type"] == "Mark1" {
print("It's Marker type 1")
}
我收到错误:
类型为“任何”的值没有下标
我在该解决方案之上的错误中搜索堆栈溢出,与我的问题无关,因为它与地点无关。
UPDATE如果仅打印:print(marker.userData!)
,我得到:
{
type = Mark1;
}
但是我无法读取If条件中的类型。
尝试一下
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
guard let data = marker.userData as? [String:Any] else{return false}
guard let type = data["type"] as? String else{return false}
if type == "Mark1" {
print("It's Marker type 1")
}
return true
}