如何从 googlemap 地址中获取 "Cityname"?
how to get just "Cityname" from googlemap address?
i need just city name cuz i will use it in weatherapi.
But after Logd coming always city and county name together.
2020-12-29 20:53:07.463 7093-7093/com.example.projekt1rain D/MapViewFragment: test5 Tombouctou, Mali
那我怎么才能得到城市呢??
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?>?
}
´´´
试试
list[0].getLocality();
如果您需要 address
中的任何其他内容,您可以查看
Android Address Class Doc
i need just city name cuz i will use it in weatherapi.
But after Logd coming always city and county name together.
2020-12-29 20:53:07.463 7093-7093/com.example.projekt1rain D/MapViewFragment: test5 Tombouctou, Mali
那我怎么才能得到城市呢??
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?>?
} ´´´
试试
list[0].getLocality();
如果您需要 address
中的任何其他内容,您可以查看
Android Address Class Doc