React Naver Maps
136

geocoder

react-naver-maps/geocoder는 SDK의 geocoder 서브모듈(naver.maps.Service)을 Promise로 사용하는 함수를 제공합니다.

<NavermapsProvider ncpKeyId="..." submodules={['geocoder']}>

사용법

import { useNavermaps } from 'react-naver-maps';
import { geocode, reverseGeocode } from 'react-naver-maps/geocoder';

function Search({ query }: { query: string }) {
  const navermaps = useNavermaps();
  const { data } = useQuery({
    queryKey: ['geocode', query],
    queryFn: () => geocode(navermaps, { query }),
  });
  return <p>{data?.addresses[0]?.roadAddress}</p>;
}

const result = await reverseGeocode(navermaps, {
  coords: { lat: 37.3595953, lng: 127.1053971 },
  orders: 'roadaddr',
});
  • 응답의 v2를 반환합니다. SDK 문서는 result를 deprecated로 표시합니다.
  • 서비스가 OK가 아닌 상태로 응답하면 GeocoderError(status, response)로 거부합니다.
  • reverseGeocode의 coords는 { lat, lng } 리터럴도 받습니다. SDK는 리터럴을 받지 않아 LatLng로 바꿔 넘깁니다.
  • 서브모듈을 불러오지 않고 호출하면 불러오는 방법을 담은 에러를 발생시킵니다.

로딩, 캐시, 재시도 같은 요청 상태는 앱에서 관리합니다. 위 예는 TanStack Query를 씁니다.

실패할 때

  • GeocoderError의 상태가 500(Service.Status.ERROR)이면 NCP 콘솔의 Application에서 Geocoding·Reverse Geocoding을 선택했는지, 키가 맞는지 봅니다. SDK는 HTTP 오류(429 등)의 이유를 알려 주지 않고 ERROR로만 답합니다.
  • reverseGeocode의 coords는 리터럴({ lat, lng })도 받습니다. SDK는 리터럴을 거절해 라이브러리가 LatLng로 바꿔 넘깁니다.
  • 좌표계 상수 Service.CoordinatesType.UTMK는 SDK 3.10.5에서 undefined입니다. 'nhn:2048'을 씁니다(좌표와 값).