'Why does my code fail with "Marker location must be assigned when added directly to map"?
This my code
import folium
map = folium.Map(Location=[38.58, -99.09],zoom_start=6,tiles='Stamen Terrain')
fg = folium.FeatureGroup(name='My Map')
fg.add_child(folium.Marker(Location=[38.2, -99.1],popup="Hi I am a Marker",icon=folium.Icon(color="green")))
map.add_child(fg)
map.save('Map1.html')
This fails with a traceback:
Traceback (most recent call last):
File "c:/Users/Godfrey Baguma/Desktop/Mapping/main.py", line 14, in <module>
map.save('Map1.html')
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\branca\element.py", line 167,
in save
html = root.render(**kwargs)
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\branca\element.py", line 319,
in render
child.render(**kwargs)
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\folium\folium.py", line 368, in render
super(Map, self).render(**kwargs)
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\folium\elements.py", line 21,
in render
PS C:\Users\Godfrey Baguma\Desktop\Mapping> & "C:/Users/Godfrey Baguma/AppData/Local/Programs/Python/Python38-32/python.exe" "c:/Users/Godfrey Baguma/Desktop/Mapping/main2.py"
2
PS C:\Users\Godfrey Baguma\Desktop\Mapping> & "C:/Users/Godfrey Baguma/AppData/Local/Programs/Python/Python38-32/python.exe" "c:/Users/Godfrey Baguma/Desktop/Mapping/main.py"
Traceback (most recent call last):
File "c:/Users/Godfrey Baguma/Desktop/Mapping/main.py", line 14, in <module>
map.save('Map1.html')
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\branca\element.py", line 167,
in save
html = root.render(**kwargs)
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\branca\element.py", line 319,
in render
child.render(**kwargs)
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\folium\folium.py", line 368, in render
super(Map, self).render(**kwargs)
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\folium\elements.py", line 21,
in render
super().render(**kwargs)
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\branca\element.py", line 643,
in render
element.render(**kwargs)
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\branca\element.py", line 643,
in render
element.render(**kwargs)
File "C:\Users\Godfrey Baguma\AppData\Local\Programs\Python\Python38-32\lib\site-packages\folium\map.py", line 302, in render
raise ValueError("{} location must be assigned when added directly to map.".format(self._name))
ValueError: Marker location must be assigned when added directly to map.
Solution 1:[1]
The reason for the error may be that you are using the function name map. Rewrite it and run it, and it will display correctly.
import folium
m = folium.Map(location=[38.58, -99.09],zoom_start=6)
fg = folium.FeatureGroup(name='My Map')
fg.add_child(folium.Marker(location=[38.2, -99.1],popup="Hi I am a Marker",icon=folium.Icon(color="green")))
m.add_child(fg)
m
Solution 2:[2]
Error : ValueError: Marker location must be assigned when added directly to map.
Solution:
It clearly indicate that the location value is not assigned . It expect "location" instead of "Location"
with this change it will run as expected.
Thanks,
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | r-beginners |
Solution 2 | Shrirang Kamble |