簡體   English   中英

獲取錯誤'org.json.JSONArray 無法轉換為 com.google.android.gms.maps.model.LatLng Android

[英]Getting error 'org.json.JSONArray cannot be converted to com.google.android.gms.maps.model.LatLng Android

Json 回應:-

{
"type" : "FeatureCollection",
"features": [
    {
        "geometry": {
            "coordinates": [ [ [ [
                -83.855020462486394,
                 36.800791838085125
            ], [
                -83.855122494686938,
                 36.800830858080204
            ], [
                -83.856076988974678,
                 36.801197800946333
            ], [
                -83.856471494201926,
                 36.801023860713194
            ], [
                -83.860159013544774,
                 36.799399831464761
            ]]]]
        }
    }
}

代碼:-

val `object` = JSONObject(x)
val features = `object`.getJSONArray("features")
for (i in 0 until features.length()) {
val featureObject = features.getJSONObject(i).getJSONObject("geometry")
if (featureObject.getString("type") == "MultiPolygon") {
    Log.e("@@@@","length...."+features.length())
   }
}


for (j in 0 until ((featureObject.getJSONArray("coordinates").get(0) as JSONArray).get(0) as JSONArray).length()) {

Log.e("coordinates@#@#", " "+((featureObject.getJSONArray("coordinates").get(0) as JSONArray).get(0) as JSONArray).get(j))

multiplePolyTempList.add(((featureObject.getJSONArray("coordinates").get(0) as JSONArray).get(0) as JSONArray).get(j) as Any)

Log.e("Anyy  ", "AnyyType ... "+((featureObject.getJSONArray("coordinates").get(0) as JSONArray).get(0) as JSONArray).get(j) as Any)

}

Exceptionjava.lang.ClassCastException:org.json.JSONArray 無法轉換為 com.google.android.gms.maps.model.LatLng

有人可以向我解釋如何將 org.json.JSONArray 轉換為 com.google.android.gms.maps.model.LatLng,我已經嘗試通過上面的方法實現但還沒有結果。

任何幫助將不勝感激。

提前致謝。

嘿,最后一個包含兩個雙精度值的 JsonArray。 您可以通過提取這些雙精度值並創建 LatLng object 將其轉換為 LatLng。例如,您可以使用以下幫助器 function。

fun convertToLatLng(jsonArray: JSONArray): LatLng {
    val lat = jsonArray.getDouble(0)
    val lng = jsonArray.getDouble(1)
    return LatLng(lat, lng)
}

暫無
暫無

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

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