如何在swift的谷歌地图sdk for ios中为地图视图添加标记 [英] How to add marker for a map view in google maps sdk for ios in swift

查看:310
本文介绍了如何在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
}

希望这对某人有帮助!

这篇关于如何在swift的谷歌地图sdk for ios中为地图视图添加标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆