Links

Locate city on map

Tags: #python #geocoding #city #coordinates #location #api
Author: Florent Ravenel​
Last update: 2023-04-12 (Created: 2023-03-24)
Description: This notebook will show how to get coordinates from a city name using Python and display it on a map.
References:

Input

Import libraries

try:
import geocoder
except:
!pip install geocoder --user
import geocoder
import pandas as pd
import plotly.express as px

Setup Variables

  • city: name of the city to get coordinates from
  • country: name of the country to help find the city
city = "Paris"
country = "France"

Model

Get coordinates from city

Using the geocoder library, we can get the coordinates from a city name.
g = geocoder.arcgis(f"{city}, {country}")
lat, lng = g.latlng
df = pd.DataFrame([{"name": city, "latitude": lat, "longitude": lng, "size": 1}])
print(f"Coordinates of {city}: {lat}, {lng}")

Output

Locate city on map

fig = px.scatter_mapbox(
df,
lat='latitude',
lon='longitude',
size='size',
text='name',
zoom=4,
mapbox_style='open-street-map'
)
fig.update_layout(
width=1200,
height=800,
)
config = {"displayModeBar": False}
fig.show(config=config)