본문 바로가기

프로그래밍 언어/Python

[Python] 구글맵 API: gmaps.geocode

반응형

구글맵 API 

ㄴ 설치 되어있지 않은 경우: 아나콘다 프롬프트웨어 다음을 실행.  

ㄴ pip install googlemaps

 

구글 클라우드의 MAPS API 페이지로 이동하여, API 키를 생성한다.

https://cloud.google.com/maps-platform/?hl=ko

콘솔로 이동 => Geocoding API 선택 => 사용자인증정보 에서 API 키 생성

 

 

 

 

API 호출(API Call)

 

import googlemaps
gmaps_key = "(본인 고유 key)" 
gmaps = googlemaps.Client(key=gmaps_key)

ㄴ 네트워크 통해서 호출하는 것

ㄴ 여태까지는 컴퓨터에 저장되어있는 라이브러리에서 불러왔었음.

 

result = gmaps.geocode('서울중부경찰서', language='ko')
result[0]['formatted_address']

 

 

- 리스트와 딕셔너리의 조합으로 되어있다.

- API를 통해서 오면, 이러한 형식으로 데이터를 주고받는 것을 Jason이라고 한다.

 

 


name_series = '서울' + df['관서명'].str[ : -2+1]+ '경찰서'
station_name  = name_series.to_list()

station_address = []

# station_name  리스트에 있는, 경찰서명칭을, 구글맵 APi에 넣어서 호출한다.

for name in station_name :  # name = '서울중부경찰서'
    result = gmaps.geocode(name,language = 'ko')
    
    if len(result) !=0:
        station_address.append(result[0]['formatted_address'])

 



반응형