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

北慕城南 提交于 2019-12-03 07:59: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!

/// 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
}
Raghib Arshi
    @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
    }
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

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