I am currently trying to dynamically display data in a Google Maps markerInfoWindow about it's specific location using the marker.userData property.
This is my function that sets marker.userData and puts markers on my map instance:
func putPlaces(places: [Place]) {
for place in places.prefix(10) {
print("*******NEW PLACE********")
let name = place.name
let address = place.address
let location = ("lat: \(place.geometry.location.latitude), lng: \(place.geometry.location.longitude)")
let locLat = place.geometry.location.latitude
let locLon = place.geometry.location.longitude
let place_id = place.place_id
let photo = place.photos
let types = place.types
let marker : GMSMarker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: locLat, longitude: locLon)
marker.icon = GMSMarker.markerImage(with: .black)
print("PLACE: \(place)")
print("PLACE.NAME: \(place.name)")
marker.userData = ["name" : "names"]
marker.map = self.mapView
}
}
I can see the specific place.name printed here^^^
This is my function (copied from GoogleMapsAPI documentation) where I am trying to access elements of the data in marker.userData:
func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? {
print("Showing marker infowindow")
print("marker.userData: \(marker.userData)")
// error here!vvv
// print("marker.userData.name: \(marker.userData.name)")
// here I will pass this data to a swiftUI view that will display the data within marker.userData
let mInfoWindow = UIHostingController(rootView: MarkerInfoWindow())
mInfoWindow.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width - 48, height: 80)
return mInfoWindow.view
}
This is the data im storing in marker.userData (taken from console when marker.userData is printed):
Optional(GMapProj.Place(geometry: GMapProj.Place.Location(location: GMapProj.Place.Location.LatLong(latitude: 42.3405476, longitude: -71.1465262)), name: "Boston Management Office", place_id: "ChIJpYqcF01444kRO8JeAqK2NuE", openingHours: Optional(GMapProj.Place.OpenNow(isOpen: false)), photos: nil, types: ["real_estate_agency", "point_of_interest", "establishment"], address: "113 Kilsyth Road # B, Brighton"))
In the last function above I try to print out the 'name' element within marker.userData, but I keep getting an error saying 'Value of type 'Any?' has no member 'name'. But i can access the name when printing right before i put the data in marker.userData...
Anyone know how I can access the data stored in marker.userData and read it's elements??
marker.userData
type is Any?. You can't use dot syntax on an object of type Any like that. You need to typecast marker.userData
to a dictionary, then access the value. Something like this.
let userData = marker.userData as? [String:String]
print("marker.userData.name": \(userData["name"])