【发布时间】:2020-12-09 00:15:14
【问题描述】:
我想在由 Android Kotlin 应用程序显示的 Google 地图上设置一个标记,作为我选择的 URL。很明显,获取 URL 内容需要在 UI 线程之外完成,而协程是这里的方法,所以我想运行几行代码来获取 URL 并将其放入内部的 BitmapDescription 对象中一个协程,然后使用该BitmapDescription 在Marker 对象上调用setIcon 以设置自定义图像。
我已经有一个Marker 和一个网址。所以我尝试了这个:
uiScope.launch(Dispatchers.IO) { // not sure this is the best way to launch in IO
val furl = URL(myURL)
val bm = BitmapFactory.decodeStream(furl.openConnection().getInputStream())
val bd = BitmapDescriptorFactory.fromBitmap(bm)
uiScope.launch(Dispatchers.Main) { // go back to UI thread; this crashes
marker.setIcon(bd)
}
}
这显然是不对的,因为它会崩溃。据我所知,获取 URL 并创建 BitmapDescriptor 似乎工作正常;一旦我有了BitmapDescriptor,我如何用它调用marker.setIcon?
【问题讨论】:
标签: android google-maps kotlin kotlin-coroutines kotlin-android-extensions