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

◇◆丶佛笑我妖孽 提交于 2019-12-09 06:59:29

问题


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.


回答1:


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!




回答2:


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



回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/31287149/how-to-add-marker-for-a-map-view-in-google-maps-sdk-for-ios-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!