Naresh Reddy M
Naresh Reddy M

Reputation: 1096

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.

Upvotes: 10

Views: 31216

Answers (5)

TharakaNirmana
TharakaNirmana

Reputation: 10353

CUSTOM MARKER

This is how I achieved it, completely programatically:

//custom markers for the map
class CustomMarker: GMSMarker {

    var label: UILabel!

    init(labelText: String, imageName: String) {
        super.init()

        let iconView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: 60, height: 60)))
        iconView.image = UIImage(named: imageName)//Assign image to ImageView
        
        
        if(labelText != "1"){
            label = UILabel(frame: CGRect(origin: .zero, size: CGSize(width: 25, height: 25)))
            label.frame.origin.x = 25;
            
            label.text = labelText
            label.layer.cornerRadius =  label.frame.width/2
            label.layer.masksToBounds = true
            label.backgroundColor = .white
            label.applyBorder(width: 0.5, color: .black)
            label.textAlignment = .center
            iconView.addSubview(label)
        }

        
        self.iconView = iconView
    }
}

Function Call:

let marker = CustomMarker(labelText: addressCount.description, imageName: mapItem.cMapType!.lowercased())

Result:

enter image description here

Upvotes: 0

Kathiresan Murugan
Kathiresan Murugan

Reputation: 2962

/// 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
}

Upvotes: 10

Raghib Arshi
Raghib Arshi

Reputation: 755

    @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
    }

Upvotes: 2

Naresh Reddy M
Naresh Reddy M

Reputation: 1096

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!

Upvotes: 28

siegy22
siegy22

Reputation: 4413

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

Upvotes: 1

Related Questions