【问题标题】:Swift - how to add custom fields to markers in Google Maps SDK?Swift - 如何将自定义字段添加到 Google Maps SDK 中的标记?
【发布时间】:2015-11-17 20:54:18
【问题描述】:

我尝试使用地图构建应用程序,并且我希望在标记中获得更多信息。文档仅提供 2 个属性 titlesnippet。看起来是这样的

let position = CLLocationCoordinate2DMake(51.5, -0.127)
let london = GMSMarker(position: position)
london.title = "London"
london.snippet = "Population: 8,174,100"
london.map = mapView

例如,我想向每个标记添加字段 rating 以显示地点的平均评分。

我该怎么做?

【问题讨论】:

  • 您可以实现自定义信息窗口,因此当用户单击标记时,它将显示有关所选地点的评分和其他信息。您可以查看this Stackoverflow answer 了解有关创建自定义信息窗口的更多详细信息。

标签: ios swift google-maps


【解决方案1】:

Google Maps SDK 在标记对象上包含一个userData 字段,请参阅link

使用您的示例,分配数据如下所示。

let mCustomData = customData(starRating: 10)  // init with a rating of 10

let position = CLLocationCoordinate2DMake(51.5, -0.127)
let london = GMSMarker(position: position)
london.title = "London"
london.snippet = "Population: 8,174,100"
london.userData = mCustomData
london.map = mapView

customData 类是这样的。

class customData{
    var starRating: Int
    init(starRating rating: Int) {
        starRating = rating
    }
}

要访问您的用户数据,只需从标记对象中检索它。

let mRating = (london.userData as! customData).starRating

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 2021-05-28
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 2015-09-26
    • 2021-12-03
    相关资源
    最近更新 更多