本文介绍了Swift - 如何将自定义字段添加到Google Maps SDK中的标记中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用地图构建应用程序,并且我想在标记中添加其他信息。文档仅提供2个属性 title 和 snippet 。它看起来像这样

  let position = CLLocationCoordinate2DMake(51.5,-0.127)
let london = GMSMarker(position:position )
london.title =伦敦
london.snippet =人口:8,174,100
london.map = mapView
以显示地点的平均评分。



我该怎么做?

解决方案

Google Maps SDK包括标记对象上的 userData 字段,请参阅。使用你的例子,这就是分配数据的样子。 p>

  let mCustomData = customData(starRating:10)//初始值为10 

let position = CLLocationCoordinate2DMake(51.5,-0.127)
let london = GMSMarker(position:position)
london.title =London
london.snippet =人口:8,174,100
伦敦。 userData = mCustomData
london.map = mapView

customData



  class customData {
var starRating:Int
init(星级评分:Int ){
starRating =评分
}
}

您的用户数据只是从标记对象中检索它。

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


I try to build app with maps, and I want to have additional info in markers. Documentation offers only 2 properties title and snippet. It looks like this

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

For example, I want to add field rating to each marker to display average rating for place.

How can I do that ?

解决方案

The Google Maps SDK includes a userData field on the marker object, see link.

Using your example this is what it would look like the following to assign the data.

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

And the customData class something like this.

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

To access the your user data just retrieve it from the marker object.

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

这篇关于Swift - 如何将自定义字段添加到Google Maps SDK中的标记中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:33