Search code examples
pythonfolium

Can't creating points using folium add_child


Python rookie here. I am making a google map API request for places, I get back a list and then i want to map the points (lat,long). I have achieved this task using the 'mapit' script, but I wanted to be able to use more features in folium ((ie)layercontrol etc). The 'for' loop I have written just maps the last item in the the list it creates. I understand why it is doing that, but don't understand how map all of them in one layer. appreciate any feedback

import folium
import pandas
import urllib3.request
import json, requests

url = "https://maps.googleapis.com/maps/api/place/textsearch/json?"
google_api = "mykey"

#google API request code
qry = input('Search query: ')
r = requests.get(url + 'query=' + qry + '&key=' + google_api)
response = r.json()
results = response['results']

for i in range(len(results)):
    location = results[i]['geometry']['location']
    lat = location['lat']
    lng = location['lng']
    nameP = results[i]['name']
    latLong = []
    latLong.append(tuple([lat,lng, nameP]))

print(latLong)


map = folium.Map(location=[39.712183, -104.998424], zoom_start=5)
point_layer = folium.FeatureGroup(name="Query Search")

point_layer.add_child(folium.CircleMarker(location=[lat, lng], radius=10,
    popup=str(nameP) + " Lat: " + str(lat) + " , Long: " + str(lng), 
    tooltip=str(nameP) + " Lat: " + str(lat) + " , Long: " + str(lng),
    fill=True,  # Set fill to True
    color='red',
    fill_opacity=1.0))..add_to(Map)

map.add_child(point_layer)
map.add_child(folium.LayerControl())  
map.save("Map1.html")

Solution

  • 2 tips before the solution:

    • Do not use map as a variable name. map is a reserved word in Python. Folium users normally use the variable name m for maps
    • Your code contains a SyntaxError. You have 2 dots in fill_opacity=1.0))..add_to(Map)

    The solution: you need to iterate as well on each lat-long pair with a for loop and then combine them on a layer. There are other ways to accomplish this without iterating (such as geoJson) but in your case this is the simplest one. Check the following code:

    import folium
    m = folium.Map(location=[39.712183, -104.998424], zoom_start=5)
    point_layer = folium.FeatureGroup(name="Query Search")
    
    latLong = [(36.314292,-117.517516,"initial point"),
               (40.041159,-116.153016,"second point"),
               (34.014757,-119.821985,"third point")]
    
    for lat,lng,nameP in latLong:
        point_layer.add_child(folium.CircleMarker(location=[lat, lng], radius=10,
            popup=str(nameP) + " Lat: " + str(lat) + " , Long: " + str(lng), 
            tooltip=str(nameP) + " Lat: " + str(lat) + " , Long: " + str(lng),
            fill=True,  # Set fill to True
            color='red',
            fill_opacity=1.0)).add_to(m)
    
    m.add_child(point_layer)
    m.add_child(folium.LayerControl())  
    m.save("Map1.html")
    

    If you want a better looking tooltip or popup, insert the text in a folium.Iframe with Html, as seen here in the fancy popup section

    Map:

    example of the map with the previous code