0

我创建了一个函数,当用户使用 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
4

0 回答 0