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

浪子不回头ぞ 提交于 2019-12-12 15:02:04

问题


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 :)


回答1:


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



回答2:


//to past markers on map from mutable array we have to use for statment

//creating marker

 let marker = GMSMarker()
for i in 0..<n{ // n= count of array

//possitioning marker,here mark,mrk,name are mutable arrays
  marker.position =CLLocationCoordinate2D(latitude:self.mark[i] as! CLLocationDegrees ,longitude:self.mrk[i] as! CLLocationDegrees ) //mutable array
  marker.title = ""
  marker.snippet = self.name[i] as! String//mutable array
  marker.map = self.mapView
}



回答3:


Based from this documentation, create a GMSMarker object to add a marker that includes a position and title, and set its map. Here is an example that demonstrates how to add a marker to an existing GMSMapView object. The marker is created at coordinates 10,10, and displays the string "Hello world" in an info window when clicked.

let position = CLLocationCoordinate2DMake(10, 10)
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = mapView

From this tutorial, in the Google Maps SDK a marker is an object of the GMSMarker class. When initializing such an object, you must specify the longitude and latitude expressed as a CLLocationCoordinate2D type.

let coordinate = CLLocationCoordinate2D(latitude: self.mapTasks.fetchedAddressLatitude, longitude: self.mapTasks.fetchedAddressLongitude)

Once the marker object has been initialized and the maps SDK knows where to place it, it’s then necessary to specify the map object to which the marker will be added to.

Check these related threads:

  • Swift - how to add custom fields to markers in Google Maps SDK?
  • How to add marker for a map view in google maps sdk for ios in swift

Hope this helps!




回答4:


Extend the GMSMarker Class

extension GMSMarker{

    fileprivate struct AssociatedKeys{
        static var anyExtraData : AnyObject?
    }
    var anyExtraData: AnyObject? {
        get {

            return objc_getAssociatedObject(self, &AssociatedKeys.anyExtraData) as? AnyObject)

        }
        set (newValue){
            if let newValue = newValue {
                objc_setAssociatedObject(
                    self,
                    &AssociatedKeys.anyExtraData,
                    newValue as AnyObject?,
                    objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
        }
    }
}

You can then use it in your GMSMarker Object:

let marker = GMSMarker()
marker.iconView = //Your UIView to resemble the new custom Marker.
marker.anyExtraData = //AnyObject


来源:https://stackoverflow.com/questions/38559871/how-to-create-my-own-gmsmarker-googlemaps-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!