1 year ago

#383971

test-img

Chris Jones

My code fails with, "ValueError: Marker location must be assigned when added directly to map.", and I don't quite understand why or how to fix it

I am working on Launch Sites Locations Analysis with Folium using a spaceX dataset, and I am trying to create Marker Clusters, because they are good way to simplify a map containing many markers having the same coordinate. I created a Marker Cluster Object and assigned a color to launch outcomes.

Also, the only columns in my dataframe right now are: "Launch Site", "Lat", "Long", "Class", and "marker_color".

Now, the question is: "for each row in spacex_df data frame, create a Marker object with its coordinate, and customize the Marker's icon property to indicate if this launch was a success or fail".

My Code:

    import folium
    import wget
    import pandas as pd
    from folium.plugins import MarkerCluster
    from folium.plugins import MousePosition
    from folium.features import DivIcon

    def assign_marker_color(launch_outcome):
        if launch_outcome == 1:
            return 'green'
        else:
            return 'red'

    spacex_csv_file = wget.download('https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/datasets/spacex_launch_geo.csv')
    spacex_df = pd.read_csv(spacex_csv_file)

    spacex_df = spacex_df[['Launch Site', 'Lat', 'Long', 'class']]
    launch_sites_df = spacex_df.groupby(['Launch Site'], as_index=False).first()
    launch_sites_df = launch_sites_df[['Launch Site', 'Lat', 'Long']]

    marker_cluster = MarkerCluster()

    spacex_df['marker_color'] = spacex_df['class'].apply(assign_marker_color)
    spacex_df.tail(10)

    site_map.add_child(marker_cluster)

    for index, record in spacex_df.iterrows():
        marker = folium.Marker(location = [record['Lat'], record['Long']],icon=folium.Icon(color='white',icon_color=record['marker_color'])).add_to(marker_cluster)    
        marker_cluster.add_child(marker)

    site_map

I have tried creating a variable called coordinates, where I can store the latitude and longitude columns of the df, then save that coordinates value to the location parameter necessary in folium.Marker(), but not even that worked. I am a little confused and would love any type of help!

Once again, my error message is, "ValueError: Marker location must be assigned when added directly to map. <folium.folium.Map at 0x7f11f92cd310>".

python

ibm-watson

folium

0 Answers

Your Answer

Accepted video resources