📌 Intro
카카오지도에서 현재 위치를 기반으로 검색하는 방법에 대해 간단하게 정리한다.
https://developers.kakao.com/docs/latest/ko/local/dev-guide#search-by-keyword
위 링크로 이동하면 다음과 같다.

이 외에도 여러가지 parameter가 있지만 내가 사용하기 위한 parameter들만 캡쳐했다.
쓰여있기도 하지만 정리해보면 다음과 같다.
- query : 필수 요소로 검색을 원하는 질의어(필수)
- x : 중심 좌표의 X 혹은 경도 값, 특정 지역을 중심으로 검색할 경우 radius와 함께 사용 가능 (radius는 반경거리)
- y : 중심 좌표의 Y 혹은 위도 값, 특정 지역을 중심으로 검색할 경우 radius와 함께 사용 가능 (radius는 반경거리)
- radius : 중심 좌표로부터의 반경 거리, 특정 지역을 중심으로 검색할 경우 중심 좌표로 쓰일 x, y와 함께 사용 가능 단위는 m이며, 최소 0에서 최대 20000까지 가능하다.
위 parameter들을 이용하여 서버와 통신하는 Interface를 만들어야 한다.
📌Interface
- KakaoAPI.java
package com.example.convenience_stores;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Query;
public interface KakaoAPI {
@GET("v2/local/search/keyword.json") // Keyword.json 의 정보를 받아옴
// 받아온 정보가 ResultSearchKeyword 클래스의 구조로 담김
Call<ResultSearchKeyword> getSearchKeyword(
@Header("Authorization") String key, // 카카오 API 인증키
@Query("query") String query, // 검색을 원하는 질의어
@Query("x") String x, // longitude
@Query("y") String y, // latitude
@Query("radius") int radius); // 반경거리
}
위와 같이 KakaoAPI라는 Interface를 만들어주자.
- Use.java
...
...
// Retrofit 생성
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
KakaoAPI api = retrofit.create(KakaoAPI.class); // 통신 인터페이스 객체로 생성
// 검색 조건 입력
Call<ResultSearchKeyword> call =
api.getSearchKeyword(
API_KEY,
name,
Double.toString(longitude),
Double.toString(latitude),
radius);
// API 서버에 요청
call.enqueue(new Callback<ResultSearchKeyword>() {
// 통신 성공 시 -> 검색 결과는 response.body()에 담김
@Override
public void onResponse(Call<ResultSearchKeyword> call, Response<ResultSearchKeyword> response) {
Log.e("TEST", "Raw : " + response.raw());
Log.e("TEST", "Body : " + response.body().documents);
// 기존에 찍혀있는 마커가 있으면 삭제
mapView.removePOIItems(mapView.getPOIItems());
for(Place document : response.body().documents){
Log.e("TEST", document.place_name);
// 마커 추가
MapPOIItem marker = new MapPOIItem();
MapPoint mapPoint = MapPoint.mapPointWithGeoCoord(Double.parseDouble(document.y), Double.parseDouble(document.x));
marker.setItemName(document.place_name);
marker.setTag(0);
marker.setMapPoint(mapPoint);
marker.setMarkerType(MapPOIItem.MarkerType.BluePin); // 기본 마커 모양
marker.setSelectedMarkerType(MapPOIItem.MarkerType.RedPin); // 클릭 시 바뀌는 모양
mapView.addPOIItem(marker); // 마커 추가
}
Log.e("TEST", "마커 추가 성공");
}
// 통신 실패 시
@Override
public void onFailure(Call call, Throwable t) {
Log.e("TEST", "통신 실패");
}
});
Toast.makeText(getApplicationContext(), "검색이 완료되었습니다.", Toast.LENGTH_LONG).show();
...
...
📌 참고
[1] https://developers.kakao.com/docs/latest/ko/local/dev-guide#search-by-keyword
[2] https://apis.map.kakao.com/android/documentation/#MapCircle
📌 Intro
카카오지도에서 현재 위치를 기반으로 검색하는 방법에 대해 간단하게 정리한다.
https://developers.kakao.com/docs/latest/ko/local/dev-guide#search-by-keyword
위 링크로 이동하면 다음과 같다.

이 외에도 여러가지 parameter가 있지만 내가 사용하기 위한 parameter들만 캡쳐했다.
쓰여있기도 하지만 정리해보면 다음과 같다.
- query : 필수 요소로 검색을 원하는 질의어(필수)
- x : 중심 좌표의 X 혹은 경도 값, 특정 지역을 중심으로 검색할 경우 radius와 함께 사용 가능 (radius는 반경거리)
- y : 중심 좌표의 Y 혹은 위도 값, 특정 지역을 중심으로 검색할 경우 radius와 함께 사용 가능 (radius는 반경거리)
- radius : 중심 좌표로부터의 반경 거리, 특정 지역을 중심으로 검색할 경우 중심 좌표로 쓰일 x, y와 함께 사용 가능 단위는 m이며, 최소 0에서 최대 20000까지 가능하다.
위 parameter들을 이용하여 서버와 통신하는 Interface를 만들어야 한다.
📌Interface
- KakaoAPI.java
package com.example.convenience_stores;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Query;
public interface KakaoAPI {
@GET("v2/local/search/keyword.json") // Keyword.json 의 정보를 받아옴
// 받아온 정보가 ResultSearchKeyword 클래스의 구조로 담김
Call<ResultSearchKeyword> getSearchKeyword(
@Header("Authorization") String key, // 카카오 API 인증키
@Query("query") String query, // 검색을 원하는 질의어
@Query("x") String x, // longitude
@Query("y") String y, // latitude
@Query("radius") int radius); // 반경거리
}
위와 같이 KakaoAPI라는 Interface를 만들어주자.
- Use.java
...
...
// Retrofit 생성
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
KakaoAPI api = retrofit.create(KakaoAPI.class); // 통신 인터페이스 객체로 생성
// 검색 조건 입력
Call<ResultSearchKeyword> call =
api.getSearchKeyword(
API_KEY,
name,
Double.toString(longitude),
Double.toString(latitude),
radius);
// API 서버에 요청
call.enqueue(new Callback<ResultSearchKeyword>() {
// 통신 성공 시 -> 검색 결과는 response.body()에 담김
@Override
public void onResponse(Call<ResultSearchKeyword> call, Response<ResultSearchKeyword> response) {
Log.e("TEST", "Raw : " + response.raw());
Log.e("TEST", "Body : " + response.body().documents);
// 기존에 찍혀있는 마커가 있으면 삭제
mapView.removePOIItems(mapView.getPOIItems());
for(Place document : response.body().documents){
Log.e("TEST", document.place_name);
// 마커 추가
MapPOIItem marker = new MapPOIItem();
MapPoint mapPoint = MapPoint.mapPointWithGeoCoord(Double.parseDouble(document.y), Double.parseDouble(document.x));
marker.setItemName(document.place_name);
marker.setTag(0);
marker.setMapPoint(mapPoint);
marker.setMarkerType(MapPOIItem.MarkerType.BluePin); // 기본 마커 모양
marker.setSelectedMarkerType(MapPOIItem.MarkerType.RedPin); // 클릭 시 바뀌는 모양
mapView.addPOIItem(marker); // 마커 추가
}
Log.e("TEST", "마커 추가 성공");
}
// 통신 실패 시
@Override
public void onFailure(Call call, Throwable t) {
Log.e("TEST", "통신 실패");
}
});
Toast.makeText(getApplicationContext(), "검색이 완료되었습니다.", Toast.LENGTH_LONG).show();
...
...
📌 참고
[1] https://developers.kakao.com/docs/latest/ko/local/dev-guide#search-by-keyword
[2] https://apis.map.kakao.com/android/documentation/#MapCircle