How to get the longitude and latitude of a city using Python?5 Jan 2025 | 4 min read In the world of programming, geolocation data is crucial for a wide range of applications, from mapping services to weather forecasting. One common task is to retrieve the longitude and latitude of a city based on its name. Python, with its rich ecosystem of libraries, offers several ways to accomplish this task. In this article, we will explore some popular methods for getting the longitude and latitude of a city using Python. Method 1: Using the Geopy LibraryGeopy is a Python library that provides geocoding services for various geographic data sources. One of the most popular services it supports is the Nominatim service, which is based on OpenStreetMap (OSM) data. Here's how you can use Geopy to get the longitude and latitude of a city: First, you need to install the Geopy library if you haven't already: Then, you can use the following Python code to get the longitude and latitude of a city: Output The latitude and longitude of New York are 40.7127281, -74.0060152 In this code, we use the Nominatim class from the geopy.geocoders module to create a geolocator object. We then use the geocode method of the geolocator object to get the location data for the specified city. If the location data is found, we extract the latitude and longitude values from the location object. Method 2: Using the Google Maps Geocoding APIGoogle Maps Platform provides a Geocoding API that allows you to convert addresses (such as city names) into geographic coordinates (latitude and longitude). To use this API, you'll need to sign up for a Google Cloud Platform account and enable the Geocoding API. Here's an example of how you can use the Google Maps Geocoding API with Python: First, you need to install the requests library if you haven't already: Then, you can use the following Python code to get the longitude and latitude of a city using the Google Maps Geocoding API: Output The latitude and longitude of New York are 40.7127753, -74.0059728 In this code, we use the requests library to send a request to the Google Maps Geocoding API. We pass the city name and our API key in the request URL. If the API returns a successful response, we extract the latitude and longitude values from the response data. Method 3: Using the Geocoder Library (for OpenStreetMap, Google Maps, Bing, etc.)The geocoder library is another popular Python library for geocoding and reverse geocoding. It supports multiple geocoding services, including OpenStreetMap, Google Maps, Bing, and others. Here's how you can use the geocoder library to get the longitude and latitude of a city: First, you need to install the geocoder library if you haven't already: Then, you can use the following Python code to get the longitude and latitude of a city using the geocoder library: Output The latitude and longitude of New York are 40.7127281, -74.0060152 In this code, we use the geocoder.osm method to create a location object for the specified city. If the location object is valid (i.e., location.ok is True), we extract the latitude and longitude values from the location object. ConclusionIn this article, we have explored three different methods for getting the longitude and latitude of a city using Python. Each method has its own advantages and disadvantages, so you can choose the one that best suits your needs. Whether you're using the Geopy library, the Google Maps Geocoding API, or the Geocoder library, you can easily retrieve geolocation data for cities and use it in your Python applications. |
Python Prediction Algorithm
Introduction: In this tutorial, we are learning about . A predictive model in Python predicts a future release based on patterns found in historical data. Essentially, by collecting and analyzing historical data, you can train a model to identify certain patterns, thus enting future sales, epidemics,...
10 min read
Sys.maxint in Python
The sys module in Python provides access to some variables used or maintained by the Python interpreter and to functions that interact strongly with the interpreter. It allows manipulation of the Python runtime environment, including system-specific parameters and functions. Understanding the sys module is crucial...
7 min read
A Complete Guide to Trino Python Client
Trino is a fast distributed SQL query engine that helps to query big data using SQL. Trino supports Python clients allowing customers to work with Trino clusters from Python scripts and applications making it straightforward to execute queries and parse outcomes. The following article will provide...
18 min read
How to Get Directory of Current Python Script
? Introduction One of the easiest to use programming language is python which is why it is widely used. When writing in Python, developers commonly find a requirement to know where their script is located. Despite appearing easy, this simple activity may prove challenging among people who...
3 min read
Pedestrian Detection Using OpenCV-Python
Pedestrian detection is a critical component of almost any contemporary application including self-driving cars or urban safety systems. Since pedestrian detection involves the use of operations such as size variation, a wealth of features in OpenCV makes it possible to perform the detection effectively while using...
8 min read
How does in operator work on list in Python
? The in operator is a powerful tool in Python for checking membership in a sequence. When applied to lists, it allows you to quickly determine whether a value exists within the list. This operator is not only useful for simple existence checks but also for...
4 min read
Chess-board Package in Python
The chess-board package in Python is a lightweight and efficient library designed to handle chessboard representations and move logic. It allows developers to easily manage and manipulate chess games programmatically by providing tools to set up and modify board positions, generate legal moves, and track game...
11 min read
OpenCV Kalman Filter in Python
Introduction: In this tutorial, we are learning about the OpenCV Kalman filter in Python. OpenCV Kalman Filter is a class of methods for implementing Kalman filter techniques. First, let us look at what the Open CV Kalman filter is used for. It is predefined for an...
4 min read
Windows Registry Access Using Python (winreg)
Windows Registry The Window Library comprises of a few primary keys, each containing subkeys and values. The fundamental keys are: HKEY_CLASSES_ROOT (HKCR): Information about enlisted applications, document affiliations, and COM objects. HKEY_CURRENT_USER (HKCU): Configuration Information for the at present signed in client. HKEY_LOCAL_MACHINE (HKLM): Configuration Information for the nearby...
8 min read
Python math.hypot() Method
The math.hypot() method is an important mathematical function found In the Python language which belongs to the general mathematical family. It is used for calculation of Euclidean distance between provided point and origin in multi-dimensional plane. This is a very simple concept that is used...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India