Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create my own GMSMarker? (GoogleMaps, iOS) - Swift

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

    }

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
        }
     }

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?

Or I could override GMSMarker constructor, to then use super(what-is-expecting-here) and then I would add the date and marker ID.

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.

Im really need to Swift and programming in general. I'd really appreciate your help :)

like image 869
Fabian Vergara Avatar asked Sep 02 '25 06:09

Fabian Vergara


1 Answers

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
like image 176
Prabhu.Somasundaram Avatar answered Sep 05 '25 02:09

Prabhu.Somasundaram