18034727885
2026-08-14 19506fce9382e60f88ee91b66742397a7c3bdafa
app/src/main/java/com/igo/user/utils/google/AMapKit.kt
@@ -6,8 +6,6 @@
import android.graphics.Bitmap
import android.graphics.Color
import android.location.LocationManager
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.View
@@ -49,6 +47,9 @@
import com.igo.user.R
import com.igo.user.utils.Const
import com.tbruyelle.rxpermissions2.RxPermissions
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
@@ -344,9 +345,9 @@
        onClick: (latLngs: MutableList<LatLng>, lineTance: Float, lineTime: Long) -> Unit
    ) {
        //https://maps.googleapis.com/maps/api/directions/json?origin=39.99709957757345,116.31184045225382&destination=39.949158391497214,116.4154639095068&sensor=false&mode=driving
        var language = LanguageUtil.getLanguage()
        var centerStr = getPointStr(centerPoint ?: arrayListOf())
        var url = "https://maps.googleapis.com/maps/api/directions/json" +
        val language = LanguageUtil.getLanguage()
        val centerStr = getPointStr(centerPoint ?: arrayListOf())
        val url = "https://maps.googleapis.com/maps/api/directions/json" +
                "?origin=" + start.latitude.toString() + "," + start.longitude.toString() +
                "&destination=" + end.latitude.toString() + "," + end.longitude.toString() +
                "&waypoints=" + centerStr +
@@ -354,22 +355,15 @@
                "&key=" + Const.google_key
        getClient(url) {
            var googleBean = Gson().fromJson<GoogleRouteBean>(it, GoogleRouteBean::class.java)
            var time = 0 //秒
            var tance = 0 //米
            var allPoint = arrayListOf<LatLng>()
            val decode =
                PolyUtil.decode(googleBean.routes?.firstOrNull()?.overview_polyline?.points ?: "")
            for (routes in googleBean.routes ?: arrayListOf()) {
                if (routes.legs.isNullOrEmpty()) {
                    Log.i(TAG, "initRouteLine: 无路线")
                    return@getClient
                }
                time = routes.legs[0].duration?.value ?: 0
                tance = routes.legs[0].distance?.value ?: 0
            val googleBean = Gson().fromJson<GoogleRouteBean>(it, GoogleRouteBean::class.java)
            if (googleBean.routes.isNullOrEmpty()) {
                return@getClient
            }
            val bean = googleBean.routes.first()
            val time = bean.legs?.firstOrNull()?.duration?.value ?: 0 //秒
            val tance = bean.legs?.firstOrNull()?.distance?.value ?: 0 //米
            val allPoint = arrayListOf<LatLng>()
            val decode = PolyUtil.decode(bean.overview_polyline?.points ?: "")
            allPoint.addAll(decode)
            onClick(allPoint, tance.toFloat(), time.toLong() / 60)
        }
@@ -448,29 +442,24 @@
        return false
    }
    fun getClient(url: String, function: (String) -> Unit) {
    fun getClient(url: String, retryCount: Int = 3, function: (String) -> Unit) {
        val client = OkHttpClient().newBuilder().build()
        val mediaType = MediaType.parse("text/plain")
        val body = RequestBody.create(mediaType, "")
        val request: Request = Request.Builder()
            .url(url)
            .method("GET", null)
            .build()
        Thread {
            try {
                val response = client.newCall(request).execute()
                Handler(Looper.getMainLooper()).post {
                    try {
                        function(response.body()?.string() ?: "")
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
        val subscribe = Observable.just(request)
            .map { request -> client.newCall(request).execute() }
            .retry(retryCount.toLong())   // 最多额外重试 3 次(加上首次共 4 次尝试)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                try {
                    function(it.body()?.string() ?: "")
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }.start()
            }, { throwable -> throwable.printStackTrace() })
    }
    /***