Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add marker for a map view in google maps sdk for ios in swift

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.

like image 926
Naresh Reddy M Avatar asked Jul 08 '15 08:07

Naresh Reddy M


1 Answers

/// 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
}
like image 68
Kathiresan Murugan Avatar answered Sep 17 '22 20:09

Kathiresan Murugan