Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用on_单击“不工作”显示ipyleaflet贴图_Python_Ipywidgets_Ipyleaflet - Fatal编程技术网

Python 使用on_单击“不工作”显示ipyleaflet贴图

Python 使用on_单击“不工作”显示ipyleaflet贴图,python,ipywidgets,ipyleaflet,Python,Ipywidgets,Ipyleaflet,我创建了一个函数,当用户使用on_click函数单击按钮时,它会生成一张地图。它没有返回任何错误,并且似乎正在运行代码,但实际的映射没有出现。我自己运行了generate_map(self)函数,它会返回映射,因此我不确定为什么在单击按钮时它不会出现。如果有人对如何解决这个问题有任何想法,我将非常感激 #Retrieve ISS location from the web url = 'http://api.open-notify.org/iss-now.json' response = url

我创建了一个函数,当用户使用on_click函数单击按钮时,它会生成一张地图。它没有返回任何错误,并且似乎正在运行代码,但实际的映射没有出现。我自己运行了generate_map(self)函数,它会返回映射,因此我不确定为什么在单击按钮时它不会出现。如果有人对如何解决这个问题有任何想法,我将非常感激

#Retrieve ISS location from the web
url = 'http://api.open-notify.org/iss-now.json'
response = urllib.request.urlopen(url)
result = json.loads(response.read())

#Define the location as latitude and longtitude
location = result['iss_position']
lat = float(location['latitude'])
lon = float(location['longitude'])

#Generate a map illustrating the current position of the ISS
center = [lat, lon] # The centre of the map will be the latitude and longtitude of the ISS
zoom = 1.5
m = Map(basemap=basemaps.Esri.WorldImagery, center=center, zoom=zoom, close_popup_on_click=False)

# Mark the position of the ISS on the map
icon1 = AwesomeIcon(name='space-shuttle', marker_color='blue', icon_color='white', spin=False)
marker = Marker(icon=icon1, location=(lat, lon), draggable=False) # Add a marker at the current location of the ISS that cannot be moved
m.add_layer(marker)

#Recieve the users latitude
input_text_lat = widgets.IntText('-35')
input_text_lat

#Recieve the users longitude
input_text_lon = widgets.IntText('150')
input_text_lon

def generate_map(self):
    user_location = input_text_lat.value, input_text_lon.value
    url ='http://api.open-notify.org/iss-pass.json'
    url = url + '?lat=' + str(input_text_lat.value) + '&lon=' + str(input_text_lon.value) #The URL requires inputs for lat and lon. The users location are the inputs here.
    response = urllib.request.urlopen(url)
    result = json.loads(response.read())

    #Convert the Unix Timestamp to date and time format
    over = (result['response'][1]['risetime'])
    date_time = datetime.datetime.utcfromtimestamp(over).strftime('%d-%m-%Y at %H:%M:%S') #These place holders define the format of the displayed date and time.
    print('The ISS will pass over you on the', date_time)

    # Mark the position of the user on the map and display a message with the date and time of the next passover.
    icon2 = AwesomeIcon(name='user', marker_color='lightred', icon_color='white', spin=False)
    marker2 = Marker(icon=icon2, location=user_location, draggable=False, title='Location') #Add  marker at the current location of the ISS that cannot be moved
    m.add_layer(marker2) # Add layer that includes the users position
    return m

button = widgets.Button(
    description='Generate map',
    disabled=False,
    button_style='success', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Click me',
)

button.on_click(generate_map)
button

通常,通过单击时使用的函数的返回值不会分配给任何对象。你需要明确地告诉笔记本显示你的地图。当您直接在笔记本中运行
generate\u map
函数时,会隐式发生这种情况。尝试将函数末尾的
return m
替换为
display(m)