🖥️ IT, 컴퓨터/🐍 Python

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

김 홍시 2025. 2. 21. 20:22
반응형

 

 

 

준비물

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:
        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", index=False)

 

 

반응형