I'm looking to build a simple Folium interactive map using Python. Within the map there will be multiple Markers, and each marker will have a JPEG / PNG in the popup.
I have successfully added a single Marker with JPEG, using this code:
csv_file = 'Location List.csv'
lat = 51.001591
long = -3.034212
png = '2.jpg'
tooltip = "Click me!"
resolution, width, height = 200, 10, 5
base_location = 'V:/CalibrationLocations/'
location_location = f'{base_location}/Location List'
image_location = f'{base_location}/Images'
extension = 'jpg'
html = '''
<h1 style="color:#61666b;size:48">MBES Data Example Image</h1>
<br>
<img src="data:image/jpeg;base64,{}">
<br>'''.format
os.chdir(location_location)
df = pd.read_csv(csv_file)
os.chdir(image_location)
def create_map():
encoded = base64.b64encode(open(png, 'rb').read()).decode('UTF-8')
iframe = IFrame(html(encoded), width=(width * resolution) + 20, height=(height * resolution) + 20)
popup = folium.Popup(iframe, max_width=2650)
icon = folium.Icon(color="red", icon="ok")
marker = folium.Marker(location=[lat, long], popup=popup, icon=icon)
m = folium.Map(location=[lat, long], zoom_start=5)
marker.add_to(m)
os.chdir(base_location)
m.save("index.html")
return m
if __name__ == '__main__':
map_layers = create_map()
I have also successfully created a Pandas DataFrame from a CSV file and added multiple markers, using this code:
def create_map():
m = folium.Map(location=[lat, long], zoom_start=5)
df.apply(lambda row: folium.Marker([row["Latitude"], row["Longitude"]], popup=row['ID']).add_to(m), axis=1)
os.chdir(base_location)
m.save("index.html")
return m
I have tried the below code, which is a hybrid of the first two functions, but it only plots one Marker, not the 4 in the CSV file. It also no longer has a JPEG / PNG in the popup.
def create_map():
encoded = base64.b64encode(open(png, 'rb').read()).decode('UTF-8')
iframe = IFrame(html(encoded), width=(width * resolution) + 20, height=(height * resolution) + 20)
popup = folium.Popup(iframe, max_width=2650)
icon = folium.Icon(color="red", icon="ok")
m = folium.Map(location=[lat, long], zoom_start=5)
df.apply(lambda row: folium.Marker([row["Latitude"], row["Longitude"]], popup=popup).add_to(m), axis=1)
os.chdir(base_location)
m.save("index.html")
return m
Has anyone succesfully plotted multiple Markers, each with a JPEG / PNG in the popup?