如何在swift的谷歌地图sdk for ios中为地图视图添加标记
尝试向Google地图添加标记,但应用程序在 addMarker()
函数调用时崩溃,异常详情如下,
Trying to add a marker to Google map,but the app is getting crashed at while addMarker()
function call,Exception details are as follows,
由于未捕获的异常 GMSThreadException 而终止应用,原因是:所有对iOS版Google地图SDK的调用都必须来自UI线程
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是全球性的,我正在尝试绘制标记。
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。
在闭包中执行 UI更新(在我的情况下 - 绘制标记) ,请记住只在主线程上获取主线程并执行UI操作。
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.
错误我所做的是,我正在尝试绘制Web服务完成块中的标记。
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
}
希望这对某人有帮助!