이제 API KEY 를 발급 받고 난 뒤의 차례이다
우선 Key 클래스를 하나 생성해 주었다
Key.kt
object Key {
const val TMAP_API = "발급받은 키"
}
그다음 POI 를 Retrofit 을 통해서 가져올것이다..!!
https://skopenapi.readme.io/reference/%EC%9E%A5%EC%86%8C%ED%86%B5%ED%95%A9%EA%B2%80%EC%83%89
우선 위의 Document 에 따르면 api 호출시 위의 JSON 처럼 나오게된다
그렇다면 우선 Model 을 생성해야겠다
굉장히 길기 때문에 GitHub 로 미리 생성해놓은 코드의 으로 첨부하겠다
👇 Model
검색결과를 RecyclerView 를 통해서 보여줄 것이기 때문에 Adapter 를 만들어준다!
👇 RecyclerView Adapter
Coroutine + Retrofit 호출
검색 버튼을 클릭시에 Retrofit2 를 호출하여 해당 키워드를 가진 정보를 Recycler 에 뿌려주는 형식으로 구현하였다
Retrofit 생성과 apiServie 클래스는 생략하도록 하겠다
주소는 아래와 같이 설정 할 것
const val TMAP_URL = "https://apis.openapi.sk.com"
const val GET_TMAP_LOCATION = "/tmap/pois"
어쨌든 여기서 포인트는 코루틴을 사용해서 api 를 호출하고
adapter 의 함수로 dataList 를 삽입 해 주는 것!
searchButton.setOnClickListener {
launch(coroutineContext) {
try {
withContext(Dispatchers.IO){
val response = RetrofitUtil.apiService.getSearchLocation(
keyword = binding.searchBarInputView.text.toString()
)
if(response.isSuccessful){
val body = response.body()
withContext(Dispatchers.Main){
Log.e("response", body.toString())
body?.let { searchResponse ->
setData(searchResponse.searchPoiInfo.pois)
}
}
}
}
}catch (e: Exception){
e.printStackTrace()
Toast.makeText(this@MainActivity, "검색안됨 : ${e.message}", Toast.LENGTH_SHORT).show()
}
}
}
//데이터를 넣어주는 함수
private fun setData(pois: Pois){
val dataList = pois.poi.map{
SearchResultEntity(
name = it.name?: "빌딩명 없음",
fullAdress = makeMainAdress(it),
locationLatLng = LocationLatLngEntity(it.noorLat, it.noorLon)
)
}
//adapter 에 set 함수 추가로 데이터를 갱신해준다
adapter.setSearchResultListener(dataList){
Toast.makeText(this, "빌딩이름 : ${it.name}", Toast.LENGTH_SHORT).show()
//지도 페이지로 전환
startActivity(
Intent(this, MapActivity::class.java).apply {
putExtra(SEARCH_RESULT_EXTRA_KEY, it)
}
)
}
}
[결과물]
반응형
'개발 > Android' 카테고리의 다른 글
[Android] Keypad 올리기 & 내리기 (0) | 2022.09.13 |
---|---|
[Android] AppBar custom 시 Background 색상 나오는 문제 (0) | 2022.08.28 |
[Android] Google Map Key 오류 (0) | 2022.08.21 |
[Android] 외부에 공유하기 (0) | 2022.08.19 |
[Android] TMap POI 사용기1 (0) | 2022.08.19 |
댓글