Template request | Bug report | Generate Data Product
Tags: #python #geocoding #city #coordinates #location #api
Author: Florent Ravenel
Description: This notebook will show how to get coordinates from a city name using Python and display it on a map.
References:
try:
import geocoder
except:
!pip install geocoder --user
import geocoder
import pandas as pd
import plotly.express as px
city
: name of the city to get coordinates fromcountry
: name of the country to help find the city
city = "Paris"
country = "France"
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}")
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)