🖥️ IT, 컴퓨터/🐍 Python

[Python] 초간단 지오코딩하기 (주소 만으로 경위도좌표 변환) :: Google Maps 지오코딩 API 활용

김 홍시 2025. 2. 21.
반응형

 

[Python] 초간단 지오코딩하기 (주소 만으로 경위도좌표 변환) :: Google Maps 지오코딩 API 활용

 

 

준비물

1. 

장소명 | 주소

2개의 열이 있는 csv파일

열 이름 위로 지정하지 않으면 코드 내에서 변경 필요! 

 

2. 구글 맵스 지오코딩 API 키

 

 

해결방법 

 

 

import pandas as pd
import requests

# Google Maps API Key 설정 (사용자의 실제 API 키 입력)
API_KEY = "키넣으십쇼"  ########### 수정 필요한 부분 ###########

# CSV 파일 불러오기
df = pd.read_csv("data.csv", encoding="utf-8")  ########### 수정 필요한 부분 ###########

# Google Maps Geocoding API를 사용하여 위도와 경도 가져오기
def get_lat_lon(address, store_name):
    base_url = "https://maps.googleapis.com/maps/api/geocode/json"
    params = {"address": address, "key": API_KEY}
    response = requests.get(base_url, params=params)
    result = response.json()

    print(f"Processing: {store_name} - {address}")

    if result["status"] == "OK":
        location = result["results"][0]["geometry"]["location"]
        return location["lat"], location["lng"]
    else:
        print(f"Geocoding failed for {address} with status {result['status']}, error message: {result.get('error_message', 'None')}")
        return None, None

# 위도, 경도 열 추가 (매장명과 주소를 함께 전달)
df["위도"], df["경도"] = zip(*df.apply(lambda row: get_lat_lon(row["주소"], row["장소명"]), axis=1))########### 수정 필요한 부분 ###########

# 결과 저장 및 출력
df.to_csv("data_with_lat_lon.csv", encoding='euc-kr', index=False) ########### 수정 필요한 부분 ###########

 

 

반응형

댓글