如何从json“ html_attribute”获取照片并在android kotlin的图像视图中显示

问题描述 投票:0回答:1
 "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : ,
               "lng" : 
            },
            "viewport" : {
               "northeast" : {
                  "lat" : ,
                  "lng" : 
               },
               "southwest" : {
                  "lat" :             ,
                  "lng" : 
               }
            }
         },
         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "                  ",
         "name" : "Singapore",
         "photos" : [
            {
               "height" : 853,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/108093408089432966223\"\u003eAdam Bolt\u003c/a\u003e"
               ],
               "photo_reference" : "CmRaAAAARrX6eoqFZJfB_kjT44C4B0aGaFV2eHZ2JzNay6BcwZRuSzK-Y1JYjOqE7s1RzUjzV_HwEuf98r5S4TRb6lUHMN2HRQq15iKLn0q2H7PxT9cERbsNeFAePMxoiUWhmgRBEhDqLn9Cft5o2sZ_YLvXxkw8GhRAW1ppgq9nxsA1nKOpls7HJxfIPw",
               "width" : 1280
            }
         ],
         "place_id" : "",
         "reference" : "",
         "scope" : "GOOGLE",
         "types" : [ "locality", "political" ],
         "vicinity" : "Singapore"
}

[从json上方尝试在地图活动的水平recyclerview中显示照片,名称和附近。成功显示名称和附近。但是如何在活动中的水平回收者视图中获取照片并在imageview中显示。在我的mapsactivity中,我正在像这样检索名称和附近位置

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_places_map)

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)

         recyclerview = findViewById<RecyclerView>(R.id.rvplaces)
        recyclerview.layoutManager =
            LinearLayoutManager(this.applicationContext, RecyclerView.HORIZONTAL, false)
        NearbyplacesArrayList = ArrayList()



    }

inner class PlacesRequest : AsyncTask<String , Int,JSONArray> (){


        override fun doInBackground(vararg params: String?): JSONArray {
            val url = URL(params[0])
            val httpURLConnection = url.openConnection() as HttpURLConnection
            httpURLConnection.requestMethod = "GET"
            httpURLConnection.connect()

            val result = httpURLConnection.inputStream.bufferedReader().readText()
            val jsonObject = JSONObject(result)
            nextToken = if (jsonObject.has("next_page_token"))
                jsonObject.getString("next_page_token")
            else ""
            return jsonObject.getJSONArray("results")

        }

        override fun onPostExecute(result: JSONArray?) {
            super.onPostExecute(result)
            requestCount++
            for(i in 0 until result!!.length()){
                val jsonObject = result.getJSONObject(i)

               var list:  placesModel = placesModel(jsonObject.getString("name"),jsonObject.getString("vicinity"))
                NearbyplacesArrayList.add(list)
                adapter = PlacesAdapter(NearbyplacesArrayList, applicationContext)
                recyclerview.adapter = adapter


            }
        }
    }

任何人都可以帮助我如何在recylcerview的图像视图中检索照片并显示图像。预期输出enter image description here

android json kotlin maps
1个回答
0
投票

以这种方式完成。得到了输出。

  var placearray = jsonObject.getJSONArray("photos")
                if(placearray == null){
                    getphoto = URL (jsonObject.getString("icon"))
                }else {
                    for (j in 0 until placearray!!.length()) {
                        var place = placearray.getJSONObject(j)
                        var placeref: String = place.getString("photo_reference")

                        getphoto =
                            URL("https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=$placeref&key=AIzaSyB5xHViy4aBF40fv1kjNGYHSCT2lYaaioA")

                        var list: placesModel = placesModel(
                            getphoto,
                            jsonObject.getString("name"),
                            jsonObject.getString("vicinity")
                        )
© www.soinside.com 2019 - 2024. All rights reserved.