Geocoding and reverse geocoding custom(user-defined) functions in Google Sheets

지오코딩 및 역지오코딩 사용자 정의 함수

1. 방향
  • Google Maps Platform의 Maps Geocoding API를 사용하지 않는다.
    • 장점은 API key가 필요하지 않다.
  • Google Apps Script의 Maps Service를 사용한다.
    • 단점은 Maps Geocoding API의 선택 옵션들을 적용할 수 없다.

2. 도움말 : Google Workspace - Apps Script - Reference - Maps Service

3. 도움말 : Google Maps Platform - Web Services - Maps Geocoding API
  • Geocoding(latitude/longitude lookup) response

    Geocoding(latitude/longitude lookup) response 캡쳐

  • Reverse geocoding(address lookup) response

    Reverse geocoding(address lookup) response 캡쳐

4. 스프레드시트의 Apps Script를 이용하여 사용자 정의 함수 생성 (초안)

스프레드시트의 Apps Script를 이용하여 사용자 정의 함수 생성하는 과정

5. 스프레드시트에 사용자 정의 함수 적용
  5.1 Dropdown을 이용한 주소 입력용 셀(B4)

Dropdown을 이용한 주소 입력용 셀(B4)

  5.2 지오코딩(위도/경도 조회)용 사용자 정의 함수 적용 : 셀(H4)

지오코딩(위도/경도 조회)용 사용자 정의 함수 적용 : 셀(H4)

  5.3 역지오코딩(주소 조회)용 사용자 정의 함수 적용 : 셀(F8)

역지오코딩(주소 조회)용 사용자 정의 함수 적용 : 셀(F8)

6. 스프레드시트의 Apps Script를 이용하여 사용자 정의 함수 완성
  6.1 지오코딩(위도/경도 조회)용 사용자 정의 함수

지오코딩(위도/경도 조회)용 사용자 정의 함수 완성
/**
 * 지오코딩(위도/경도 조회)
 * Geocoding (latitude/longitude lookup)
 * 
 * @param {String}  address 주소
 * @returns {Number[]}  [위도, 경도]
 * @customFunction
 */
function GEO_COORDINATES(address) {
  const response = Maps.newGeocoder().geocode(address);
  const { status: status_message, results: [results_data = {}] = [] } = response;

  if (status_message === 'ZERO_RESULTS') {
    return 'Coordinates not found!';
  }

  const { geometry: { location: { lat, lng } } = {} } = results_data;

  // Returns String
  // return `${lat}, ${lng}`;
  return [[lat, lng]];
}

  6.2 역지오코딩(주소 조회)용 사용자 정의 함수 완성

역지오코딩(주소 조회)용 사용자 정의 함수 완성
/**
 * 역지오코딩(주소 조회)
 * Reverse geocoding (address lookup)
 * 
 * @param {Number}  latitude  위도
 * @param {Number}  longitude 경도
 * @returns {String}  주소
 * @customFunction
 */
function GEO_ADDRESS(latitude, longitude) {
  const response = Maps.newGeocoder().reverseGeocode(latitude, longitude);
  const { status: status_message, results: [results_data = {}] = [] } = response;

  if (status_message === 'ZERO_RESULTS') {
    return 'Address not found!';
  }

  return results_data.formatted_address;
}

7. 지오코딩(위도/경도 조회) 및 역지오코딩(주소 조회) 사용자 정의 함수 테스트
주소를 이용한 지오코딩(위도/경도 조회) 및 역지오코딩(주소 조회) 사용자 정의 함수 테스트
주소

지명을 이용한 지오코딩(위도/경도 조회) 및 역지오코딩(주소 조회) 사용자 정의 함수 테스트
지명

  (참고) 역지오코딩(주소 조회) 결과에 사용할 언어 선택법 : Method setLanguage(language) 적용으로 가능


  (참고) 수정 코드 : 한국어 결과 표출
const response = Maps.newGeocoder().setLanguage('ko').reverseGeocode(latitude, longitude);


예제시트를 열려면 여기를 클릭하여 파일을 연다. '메뉴 > 파일 > 사본 만들기'를 클릭한다.(In the menu, click File > Make a copy)
예제시트에 액세스할 수 없는 경우 여기를 마우스 오른쪽 버튼으로 클릭하고 '시크릿 창에서 링크 열기' 선택.

Comments