簡體   English   中英

如何替換此代碼中的 AsyncTask 以根據 api 調用字符串查找位置

[英]How to replace AsyncTask in this code to find places according to a api call string

我在我的個人 android 項目中遇到了這個問題:我想使用 google places api 調用來顯示和標記地圖上附近和特定的地方,但它不能按預期工作,因為 asyncTasks 已被棄用。

如何替換已棄用的類 AsyncTask ? 提前致謝:

class MapsActivity : FragmentActivity(),OnMapReadyCallback {
    var context : Context? = null
    var mMap : GoogleMap? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_maps)
        context = this@MapsActivity
        val mapFragment=supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
        mapFragment?.getMapAsync(this)

    }


    override fun onMapReady(p0: GoogleMap) {
        Toast.makeText(this,"Map is Ready",Toast.LENGTH_LONG).show()
        mMap = p0
        ////////////hitapi(this@MapsActivity,18.55,73.94,10000,"")

    }

    private inner class hitapi : AsyncTask<Void, Void, String>
    {

        var context: Context? = null
        var lat: Double? = null
        var long: Double? = null
        var rad: Int? = null
        var type: String? = null
        constructor(context: Context, lat: Double, long: Double, rad: Int, type: String) {
            this.context = context
            this.lat = lat
            this.long = long
            this.rad = rad
            this.type = type
        }
        override fun doInBackground(vararg p0: Void?): String {
            Toast.makeText(context,"doInBackground",Toast.LENGTH_LONG).show()
            return GooglePlacesApis().getplacesJson(context as Context, lat as Double, long as Double, rad as Int, type as String)
        }

        override fun onPostExecute(result: String?) {
            Toast.makeText(context,"onPostExecute",Toast.LENGTH_LONG).show()
            super.onPostExecute(result)
            val gson = GsonBuilder().create()
            val root = gson.fromJson(result,PlacesRootClass::class.java)

        } }
    public fun addMarkers(root :PlacesRootClass)
    {
        Toast.makeText(this, "blah", Toast.LENGTH_SHORT).show()
        for(result in root.results)
        {
            val p = LatLng(result.geometry?.location?.lat!!,result.geometry?.location?.lng!!)
            mMap!!.addMarker(MarkerOptions().position(p).title(result.name))
        }
        mMap!!.moveCamera(CameraUpdateFactory.newLatLng(LatLng(18.55,73.94)))
        mMap!!.animateCamera(CameraUpdateFactory.zoomTo(15f))
    }

}

使用 kotlin 協程來做你在 doInBackground 方法中所做的事情:

fun hitApi() {
  GlobalScope.launch(Dispatchers.IO) {
    val result = GooglePlacesApis().getplacesJson(this, lat, long, rad, type)
    val root = gson.fromJson(result, PlacesRootClass::class.java)
    ...
  }
}

並在 build.gradle 中添加依賴:

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9")
}

在此處閱讀有關協程的更多信息: https ://developer.android.com/kotlin/coroutines

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM