도시의 경도와 위도를 가져오기 위해 geopy를 사용합니다. 기준 치수. 지역 타사 지오코더 및 기타 데이터 소스를 사용하여 주소, 도시, 국가 등의 좌표를 찾습니다.
우선 지리적 모듈이 설치되었습니다 -
pip install geopy
다음 예에서는 지명을 사용합니다. 지오코더를 사용하여 "하이데라바드" 도시의 경도와 위도를 찾습니다.
단계 -
-
Nominatim 지오코더 가져오기 지역에서 모듈.
-
Nominatim API를 초기화하고 지오코드를 사용합니다. 입력 문자열의 위치를 가져오는 메소드입니다.
-
마지막으로 location.latitude로 위치의 위도와 경도를 가져옵니다. 및 location.longitude .
예시 1
# Import the required library
from geopy.geocoders import Nominatim
# Initialize Nominatim API
geolocator = Nominatim(user_agent="MyApp")
location = geolocator.geocode("Hyderabad")
print("The latitude of the location is: ", location.latitude)
print("The longitude of the location is: ", location.longitude) 출력
콘솔에 다음 출력을 인쇄합니다 -
The latitude of the location is: 17.360589 The longitude of the location is: 78.4740613
이 예에서는 예 1의 반대 작업을 수행하겠습니다. 먼저 좌표 집합을 제공하고 이 좌표가 나타내는 도시, 주 및 국가를 찾습니다. 콘솔에서 출력을 인쇄하는 대신 출력을 표시하기 위해 4개의 레이블이 있는 tkinter 창을 만듭니다.
단계 -
-
Nominatium API를 초기화합니다.
-
geolocator.reverse() 사용 기능을 수행하고 좌표(위도 및 경도)를 제공하여 위치 데이터를 가져옵니다.
-
location.raw['address']를 사용하여 위치의 주소를 가져옵니다. address.get()을 사용하여 데이터를 탐색하여 도시, 주 및 국가를 찾습니다. .
-
tkinter 창 내부에 레이블을 만들어 데이터를 표시합니다.
예시 2
from tkinter import *
from geopy.geocoders import Nominatim
# Create an instance of tkinter frame
win = Tk()
# Define geometry of the window
win.geometry("700x350")
# Initialize Nominatim API
geolocator = Nominatim(user_agent="MyApp")
# Latitude & Longitude input
coordinates = "17.3850 , 78.4867"
location = geolocator.reverse(coordinates)
address = location.raw['address']
# Traverse the data
city = address.get('city', '')
state = address.get('state', '')
country = address.get('country', '')
# Create a Label widget
label1=Label(text="Given Latitude and Longitude: " + coordinates, font=("Calibri", 24, "bold"))
label1.pack(pady=20)
label2=Label(text="The city is: " + city, font=("Calibri", 24, "bold"))
label2.pack(pady=20)
label3=Label(text="The state is: " + state, font=("Calibri", 24, "bold"))
label3.pack(pady=20)
label4=Label(text="The country is: " + country, font=("Calibri", 24, "bold"))
label4.pack(pady=20)
win.mainloop() 출력
다음 출력을 생성합니다 -
