10

Trying to add a marker to Google map,but the app is getting crashed at while addMarker() function call,Exception details are as follows,

Terminating app due to uncaught exception 'GMSThreadException', reason: 'All calls to the Google Maps SDK for iOS must be made from the UI thread'

FYI vwGogleMap is global and in a function I'm trying to plot 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 appreciated,

TIA.

Naresh Reddy M
  • 1,066
  • 1
  • 10
  • 27
  • Normally, it means you put the code in the background thread instead of UI thread. Could you provide more code, please? So, I could see where do you put this code in your file. – thanyaj Jul 08 '15 at 08:26
  • in view controller at the top class ViewController: UIViewController { var Gmap : GMSMapView? override func viewDidLoad() { super.viewDidLoad() self. addMarker() } func addMarker { var position = CLLocationCoordinate2DMake(17.411647, 78.435637) var marker : GMSMarker = GMSMarker(position: position) as GMSMarker marker.title = "Hello World" marker.map = Gmap } } – Naresh Reddy M Jul 08 '15 at 08:33

4 Answers4

26

When performing UI Updates in closures(In my case - Plotting markers),Do remember to get main thread and perform UI Operations on main thread only.

Mistake what i did is,I'm trying to plot markers in 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
}

Hope this helps for someone!

Naresh Reddy M
  • 1,066
  • 1
  • 10
  • 27
9
/// Marker - Google Place marker
let marker: GMSMarker = GMSMarker() // Allocating Marker

 marker.title = "Title" // Setting title
 marker.snippet = "Sub title" // Setting sub title
 marker.icon = UIImage(named: "") // Marker icon
 marker.appearAnimation = .pop // Appearing animation. default
 marker.position = location.coordinate // CLLocationCoordinate2D

DispatchQueue.main.async { // Setting marker on mapview in main thread.
   marker.map = mapView // Setting marker on Mapview
}
Kathiresan Murugan
  • 2,443
  • 3
  • 22
  • 39
2
    @IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
        super.viewDidLoad()

        mapView.camera = GMSCameraPosition.camera(withLatitude: 18.514043, longitude: 57.377796, zoom: 6.0)
        let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: 18.514043, longitude: 57.377796))
        marker.title = "Lokaci Pvt. Ltd."
        marker.snippet = "Sec 132 Noida India"
        marker.map = mapView
    }
Vineeth Sai
  • 3,262
  • 7
  • 20
  • 31
Raghib Arshi
  • 607
  • 6
  • 11
1
var marker = GMSMarker()
marker.location = location
marker.title = location.name
marker.snippet = "Info window text"
marker.map = mapView

The location property must be set with a CLLocationCoordinate2D

To make a new locationcoordinate use this:

 CLLocationCoordinate2D(latitude: CLLocationDegrees(<latitude>), longitude: CLLocationDegrees(<longitude>))

It's really simple.. Make sure your map is initialized by doing that

siegy22
  • 4,135
  • 2
  • 21
  • 41