- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the longitude and latitude of a city using Python?
To get the longitude and latitude of a city, we will use the geopy module. geopy uses third-party geocoders and other data sources to locate the coordinates of addresses, cities, countries, etc.
First of all, make sure the geopy module is installed −
pip install geopy
In the following example, we will use the Nominatim geocoder to find the longitude and latitude of the city "Hyderabad".
Steps −
Import Nominatim geocoder from geopy module.
Initialize the Nominatim API and use the geocode method to get the location of the input string.
Finally, get the latitude and longitude of the location by location.latitude and location.longitude.
Example 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)
Output
It will print the following output on the console −
The latitude of the location is: 17.360589 The longitude of the location is: 78.4740613
In this example, let's do the opposite of Example 1. We will start by providing a set of coordinates and find the city, state, and country those coordinates represent. Instead of printing the output on the console, we will create a tkinter window with four labels to display the output.
Steps −
Initialize the Nominatium API.
Use the geolocator.reverse() function and supply the coordinates (latitude and longitude) to get the location data.
Get the address of the location using location.raw['address'] and traverse the data to find the city, state, and country using address.get().
Create labels inside a tkinter window to display the data.
Example 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()
Output
It will produce the following output −
- Related Articles
- How to get the current location latitude and longitude in iOS?
- How to get current location latitude and longitude in Android?
- How to get a complete address from latitude and longitude on Android Kotlin?
- How do I get an address latitude-longitude using HTML5 Geolocation or Google API?
- How to find the latitude and longitude from address in Android?
- How to complete address from latitude and longitude on Android?
- How to use HTML5 Geolocation Latitude/Longitude API?
- How can I find the latitude and longitude from address on Android using Kotlin?
- How to track the current location (Latitude and Longitude) in an android device using Kotlin?\n
- Latitude and longitude of the user's position in JavaScript?
- How to get latitude from Network provider in android?
- How to get longitude from Network provider in android?
- How to get stat of a file using Python?
- Thomas covers a total distance of 1056 km from city A to city C through city B. If the distance from city A to city B is 543.7 km, find the distance between city B and city C.Express the answer as a decimal.
- How to get the center of a set of points using Python?
