Reputation: 11
I have an app that shows a marker using the Google Maps API, but sometimes it just doesn't show the marker. I've checked the debugger and the code is being executed, but nothing shows up. Here is the code:
func showPickupMarker(lat: Double, lon:Double){
pickupMarker = GMSMarker(position: CLLocationCoordinate2D(latitude: lat, longitude: lon))
pickupMarker!.icon = GMSMarker.markerImage(with: UIColor.init(colorLiteralRed: 0/255, green: 164/255, blue: 223/255, alpha: 1))
pickupMarker!.map = self.mapView
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude:
lon, zoom: 15.0)
let update = GMSCameraUpdate.setCamera(camera)
self.mapView.moveCamera(update)
}
It works most of the time, but every once in a while it just...doesn't. The map won't move, and the pin doesn't show up. The function is being called from another ViewController if that makes a difference.
I also checked if the marker was nil when it wasn't showing up, but it's not. According to my app there is a marker, but it's not shown.
FOUND SOLUTION I was accidentally loading another GMSMapView on top of the original in locationManager didChangeAuthorizationStatus. Apparently this function just randomly gets called, hence why it didn't happen every time...
Upvotes: 0
Views: 650
Reputation: 1119
Make sure that you run the method on the main thread. Try changing the method implementation into:
DispatchQueue.main.async {
...
}
Upvotes: 1