我创建了一个函数,该函数使用用户的纬度和经度生成地图,并在下次国际空间站将在用户位置上方时返回。我创建了一个按钮,使用 on_click 函数应该使用 lat 和 lon 作为变量生成地图,但显然,地图对象是不可调用的。我使用callable
generate_map 函数的函数对此进行了测试,它返回 True 表示该函数是可调用的。下面是代码。任何提示将非常感谢。
#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():
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