Links

Locate address 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 an address 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

  • address: address to get coordinates from
address = "Sayyaji Rao Rd, Agrahara, Chamrajpura, Mysuru, Karnataka 570001"

Model

Get coordinates from city

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

Output

Locate city on map

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