Interactive Geo location pointer using Python
Hey guys, here am gonna show you how to create an interactive map/geolocation pointer using python.
Folium
The folium module is one of the most important libraries in python that creates various types of maps.
It creates a real-time map that can be viewed, adjust by zoom in or zoom out and can pin a location based on the geolocation values.
By default , a HTML file is created to generate a map. But in the case of executing a python program in jupyter notebook, it creates an inline map, which can be viewed right after the program gets executed.
Prerequisite
As everyone knows, any python module can be installed using pip, so use
pip install folium
The above line of code installs folium and gets added to your previous pip list.
Folium and its use cases
Prior to the use of the folium module, installation is mandated.
After installation , the module can be imported by the help of import statement.
import folium
After grabbing all its functionalities into your program, next the beginner of folium module must know about its inbuilt methods to be invoked in respective cases.
To know about the list of built-in methods in folium module, just drop a line of code,
dir(folium)
folium.Map(location,zoom_start)
In the above code , Map( ) function in folium takes two arguments: location and zoom_start.
location can be assigned with any latitude and longitude values to get a specific location or it can be generally specified as “location = center”.
zoom_start can be any numeric value that specifies how zoomed the map should be viewed after execution.
It can be zoomed in or zoomed out according to the user need.
Example:
map1 = folium.Map(location=[11.026580670779277, 76.95986049867513],zoom_start=15)
folium.Marker(location, popup)
In the above code, folium.Marker() is used for pinpointing any location based on the location value specified as arguments.
popup is used for displaying a popup message like location name over the pinned location.
Example:
folium.Marker(location=[11.026580670779277, 76.95986049867513],popup=”Sri Vignesh Rice and Grocery shop”).add_to(map1)
Saving a map
Thus generated map can be saved for future reference using ,
variablename.save(“filename.html”)
This creates an HTML file that has the map generated before.
It can be further modified, in case of any need for modifications.
Example:
map1.save(“map1.html”)
Displaying inline map
Software like Jupyter Notebook has inbuilt visual representation for python programming.
Thus the map generated by the user in Jupyter Notebook can be viewed immediately right after its creation using,
display(variablename)
It displays the map generated earlier, as an inbuilt map inside the console.
Example:
display(map1)
Code Snippet
import folium
map1 = folium.Map(location=[11.026580670779277, 76.95986049867513],zoom_start=15)
folium.Marker(location=[11.026580670779277, 76.95986049867513],popup=”Sri Vignesh Rice and Grocery shop”).add_to(map1)
map1.save(“map1.html”)
display(map1)
Output
If you can’t then who can??