How to add a map view of Google Maps for iOS in SWIFT

I tried to add a marker on Google Maps, but the application crashed when the addMarker() function was called. The exception details are as follows,

The catched exception’GMSThreadException’ terminates the application. The reason is:’All calls to the iOS Google Maps SDK must come from the UI thread’

For reference only, vwGogleMap is global, and I am trying to draw a marker.

func addMarker() -> Void
{
var vwGogleMap: GMSMapView?
var position = CLLocationCoordinate2DMake(17.411647,78.435637)
var marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = vwGogleMap
}

Any help would be greatly appreciated ,

TIA.

When performing UI updates in a closure (in In my case-draw markers), please remember to only get the main thread and perform UI operations on the main thread.

The error I did is, I am trying to complete the block in the web service Draw markers in the middle.

dispatch_async(dispatch_get_main_queue(),
{
var position = CLLocationCoordinate2DMake(17.411647,78.435637)
var marker = GMSMarker (position: position)
marker.title = "Hello World"
marker.map = vwGogleMap
})

// For swift 3.0 support.
/ / 1. Get Main thread
DispatchQueue.main.async
{
// 2. Perform UI Operations.
var position = CLLocationCoordinate2DMake(17.411647,78.435637)
var marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = vwGoogleMap
}

I hope this helps someone!

An attempt was made to add a marker on Google Maps, but the application crashed when the addMarker() function was called. The exception details are as follows,

< p>The application is terminated due to the uncaught exception’GMSThreadException’. The reason is:’All calls to the iOS Google Maps SDK must come from the UI thread’

For reference only, vwGogleMap is global, I am working on Try to draw the marker.

func addMarker() -> Void
{
var vwGogleMap: GMSMapView?
var position = CLLocationCoordinate2DMake(17.411647, 78.435637)
var marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = vwGogleMap
}

Any help , Would be greatly appreciated,

TIA.

When performing UI updates in closures (in my case-draw markers) , Please remember to only get the main thread and perform UI operations on the main thread.

The error what I did is, I am trying to draw the marker in the web service completion block.

dispatch_async(dispatch_get_main_queue(),
{
var position = CLLocationCoordinate2DMake(17.411647,78.435637)
var marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = vwGogleMap
})

// For swift 3.0 support.
// 1. Get Main thread
DispatchQueue.main.async
{
// 2. Perform UI Operations.
var position = CLLocationCoordinate2DMake(17.411647,78.435637)
var marker = GMSMarker(position: position)
marker.title = "Hello World"
marker. map = vwGoogleMap
}

I hope this helps someone!

Leave a Comment

Your email address will not be published.