尝试向Google地图添加标记,但应用程序在addMarker()
函数调用时崩溃,异常详情如下,
由于未捕获的异常终止应用<&strong> GMSThreadException &#39;,原因:&#39;所有对iOS版Google地图SDK的调用都必须来自UI线程&#39 ;
仅供参考vwGogle地图是全球性的,我试图绘制标记。
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
}
任何帮助将不胜感激,
TIA。
答案 0 :(得分:19)
在闭包中执行 UI更新(在我的情况下 - 绘制标记)时,请记住仅在主线程上获取主线程并执行UI操作。
错误我所做的是,我正在尝试在Web服务完成块中绘制标记。
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
}
希望这对某人有帮助!
答案 1 :(得分:1)
var marker = GMSMarker()
marker.location = location
marker.title = location.name
marker.snippet = "Info window text"
marker.map = mapView
必须使用location
CLLocationCoordinate2D
属性
要创建一个新的locationcoordinate,请使用:
CLLocationCoordinate2D(latitude: CLLocationDegrees(<latitude>), longitude: CLLocationDegrees(<longitude>))
这很简单.. 确保通过执行
来初始化地图答案 2 :(得分:0)
@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
}
答案 3 :(得分:0)
/// 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
}