Search code examples
androidkotlinandroid-asynctaskgoogle-places-apikotlin-coroutines

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


I'm getting this issue on my personal android project: i want to use google places api call to show and mark nearby and specific places on map but it does not work as intended because asyncTasks are deprecated.

How to replace the deprecated class AsyncTask ? Thanks in advance:

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))
    }

}

Solution

  • Use kotlin coroutines to do the stuff you're doing in doInBackground method:

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

    and add dependency in build.gradle:

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

    read more about coroutines here: https://developer.android.com/kotlin/coroutines