【问题标题】:how to get just "Cityname" from googlemap address?如何从谷歌地图地址中获取“城市名称”?
【发布时间】:2023-03-20 21:31:02
【问题描述】:

我只需要城市名称,因为我将在 weatherapi 中使用它。

但是在 Logd 之后总是城市和县名一起出现。

2020-12-29 20:53:07.463 7093-7093/com.example.projekt1rain D/MapViewFragment: test5 马里通布图

那么我怎样才能得到城市呢??

override fun onMapReady(map: GoogleMap?) {

        if (map != null) {
            nMap = map
        }

        map?.let {
            nMap = it

            nMap.setOnInfoWindowClickListener { markerToDelete ->
                Log.i(TAG, "onWindowsClickListener - Delete Thismarker")
                markers.remove(markerToDelete)
                markerToDelete.remove()
            }


            nMap.setOnMapLongClickListener { latlng ->

                Log.i(TAG, "onMapLongClickListener" + latlng)

                Toast.makeText(
                    requireContext(),
                    "this is toast message" + latlng,
                    Toast.LENGTH_SHORT
                ).show()
                showAlertDialog(latlng)

                // i need just city name cuz i will use it in weatherapi.
                // But after Logd coming always city and county name together.
                val address= getAddress(latlng.latitude, latlng.longitude)
                retrofitResponse(address)

                Log.d(TAG,"test5 $address")
                Toast.makeText(requireContext(),"test"+address,Toast.LENGTH_LONG).show()
            }
        }
    }


    private fun getAddress(lat: Double, lng: Double): String {
        val geocoder = Geocoder(requireContext())
        val list = geocoder.getFromLocation(lat, lng, 1)
        return list[0].getAddressLine(0)
        //val stateName: String = addresses[0].getAddressLine(1)
    }
´´´

接口 CallWeatherApi {

@GET("weather")
fun getWeather(@retrofit2.http.Query("q") cityname: String?,
               @retrofit2.http.Query("appid") apiKey: String?
):Call<CurrentWeatherResponse?>?

} '''

【问题讨论】:

    标签: android kotlin retrofit google-maps-android-api-2


    【解决方案1】:

    试试

    list[0].getLocality();
    

    如果您需要address 的其他任何内容,可以查看 Android Address Class Doc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 2012-04-04
      • 1970-01-01
      • 2016-11-21
      • 2018-01-16
      • 1970-01-01
      相关资源
      最近更新 更多