问题描述
我想创建自己的标记,因为我需要跟踪有关它们的额外信息(例如,添加日期,标记ID)...
I would like to create my own markers because I need to track extra information about them (for example, date added, marker ID)...
所以我创建了另一个类,如下所示:
So I created another class, as follows:
class myMarker: GMSMarker {
var markerID: Int
var addedDate: NSDate
}
但是我不知道下一步该怎么做...我应该创建一个"init"方法来填充我的markerId和addedDate吗?或者我应该重写GMSMarker构造函数(不是"init")可以是
But I don't know what do to next... should I create an "init" method to populate my markerId and addedDate? or I should override the GMSMarker constructor (which is not "init")It can be
class myMarker: GMSMarker {
var markerID: Int
var addedDate: NSDate
var myMarker: GMSMarker
init(marID: Int, date: NSDate, GMSMarker: markerwithoutDateNorId){
self.markerID = marID
self.addedDate = date
self.myMarker = markerwithoutDateNorId
}
}
我真的不知道如何实现这一点.也许我什至不需要实现构造函数,而是使用GMSMarker构造函数(我相信我继承了),然后使用set方法设置标记ID和Date?
I really don't have an idea how to implement this. Maybe I don't even need to implement a constructor, but use the GMSMarker constructor (which I believe I inherited) and then use set methods to set marker ID and Date?
或者我可以重写GMSMarker构造函数,然后使用super(在这里是什么),然后添加日期和标记ID.
Or I could override GMSMarker constructor, to then use super(what-is-expecting-here) and then I would add the date and marker ID.
我确实需要创建自己的标记类型,因为我需要跟踪这些内容……例如,我希望将来删除所有早于1周的标记...并且如果我不跟踪标记的日期,那我将是不可能的.
I really need to create my own type of marker, because I need to keep track of these things... for example, I would like, in a future, remove all markers older than 1 week for example... and if I don't keep track of markers date, that would me impossible.
我真的真的需要Swift和编程.非常感谢您的帮助:)
Im really need to Swift and programming in general. I'd really appreciate your help :)
推荐答案
还有另一种方法,GMSMarker具有 userData 属性,可以接受AnyObject.
There is an another approach, GMSMarker has userData property which can accept AnyObject.
var info: Dictionary<String, AnyObject>?
info["markerID"] = 0
info["addedDate"] = NSDate()
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude:latitude, longitude:longitude)
marker.map = nil
marker.tracksInfoWindowChanges = false
marker.map = self.mapView
marker.userData = info
这篇关于如何创建自己的GMSMarker? (GoogleMaps,iOS)-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!