How to Add Custom Data to a Marker (Google Maps API)
I am trying to figure out a way to add custom properties / data for my marker.
What i used
- Marker.Title (store name)
- Marker.Snippet (Repository Object ID)
- Marker.Icon (Save)
- Marker.userData (for storing the image)
Additional information I would like to keep for displaying in InfoWindow
-
Description (string)
-
Number (Int)
It would be nice if there were better ways to store and retrieve custom data / properties for display in the infowindow. For now, any suggestions would be appreciated.
Here is a snippet of what I have at the moment: Setting marker data:
marker.title = object["objectTitle"] as! String
marker.snippet = objecti.objectId
marker.icon = UIImage(named:"markerImage")
marker.userData = UIImage(data:imageData!)
Information window:
func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! {
var infoWindow :CustomInfoWindow = NSBundle.mainBundle().loadNibNamed("eventInfo", owner: self, options: nil)[0] as! CustomInfoWindow
infoWindow.eventTitle.text = marker.title
infoWindow.Image.image = marker.userData as? UIImage
marker.userData = UIImage(data:imageData!)
marker.snippet = object.objectId
return infoWindow
}
source to share
I came across your question, just starting to learn Swift, but you think something like this will work:
var myData = Dictionary<String, Any>()
myData["image"] = UIImage(data:imageData!)
myData["description"] = "The description"
myData["number"] = 1
marker.userData = myData
It is a method of storing several types of data that I recently learned about.
source to share
I created a struct that has all the data I need and put it in marker.userdata as it supports any type.
let data = structName(lat: place.lat, long: place.long) // initialize struct
marker.userData = data
Aceess is like this:
let markerLat = (marker.userData as! structName).lat
let markerLong = (marker.userData as! structName).long
source to share