一、地理位置
你所在的經緯度是{{latitude}}, {{longitude}}。這是一個非常重要的信息,因為它直接影響到你身處的地理位置。如果你正在開發一個應用程序,比如一個地圖應用程序,那麼你可以使用這些信息來讓你的應用程序知道你的實際位置並與之交互。
二、天氣情況
當前位置的經緯度還可以讓你了解當前氣象狀況。你可以使用開源天氣 API,從中獲得天氣數據。
import requests
from pprint import pprint
# Enter your API key here
api_key = "Your_API_Key"
# base_url variable to store url
base_url = "http://api.openweathermap.org/data/2.5/weather?"
# Give city name
city_name = input("Enter city name : ")
# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
# get method of requests module
# return response object
response = requests.get(complete_url)
# json method of response object convert
# json format data into python format data
x = response.json()
# Now x contains list of nested dictionaries
# Check the value of "cod" key is equal to
# "404", means city is found otherwise,
# city is not found
if x["cod"] != "404":
# store the value of "main"
# key in variable y
y = x["main"]
# store the value corresponding
# to the "temp" key of y
current_temperature = y["temp"]
# store the value corresponding
# to the "pressure" key of y
current_pressure = y["pressure"]
# store the value corresponding
# to the "humidity" key of y
current_humidiy = y["humidity"]
# store the value of "weather"
# key in variable z
z = x["weather"]
# store the value corresponding
# to the "description" key at
# the 0th index of z
weather_description = z[0]["description"]
# print following values
print("Temperature (in Kelvin unit) = " +
str(current_temperature) +
"\nAtmospheric pressure (in hPa unit) = " +
str(current_pressure) +
"\nHumidity (in percentage) = " +
str(current_humidiy) +
"\nDescription = " +
str(weather_description))
else:
print(" City Not Found ")
三、附近地點
你也可以使用當前位置的經緯度信息,通過 Foursquare API,獲取你身處的位置周圍的興趣點信息。這些位置包括餐館、酒吧和商店等。
import requests
# set up API url
url = 'https://api.foursquare.com/v2/venues/explore'
# set up API params
params = dict(
client_id='YOUR_CLIENT_ID',
client_secret='YOUR_CLIENT_SECRET',
v='20180323',
ll='40.7243,-74.0018',
query='coffee',
limit=1
)
# Get the response
resp = requests.get(url=url, params=params)
# Print the response
print(resp.json())
四、時區信息
當前位置的經緯度將直接影響到你說處的時區信息。你可以使用 Google Time Zone API 獲取時區信息:
import requests
# set up API url
url = 'https://maps.googleapis.com/maps/api/timezone/json'
# set up API params
params = {
"location": "37.774929,-122.414187",
"timestamp": "1331766000",
"key": "YOUR_API_KEY"
}
# Get the response
resp = requests.get(url=url, params=params)
# Print the response
print(resp.json())
五、高程信息
最後,你還可以使用當前位置的經緯度獲取海拔高度信息。你可以使用 Google Elevation API。使用它可以輕鬆地獲取經緯度所在地的海拔高度。
import requests
# Set up API url
url = 'https://maps.googleapis.com/maps/api/elevation/json'
# Set up API params
params = {
"locations": "39.7392,-104.9903",
"key": "YOUR_API_KEY"
}
# Get the response
resp = requests.get(url=url, params=params)
# Print the response
print(resp.json())
總結
你所處的經緯度將可用於多種應用程序。你已經學習如何使用開源 API 來獲取你所處的地理位置、天氣情況、附近地點、時區信息和高程信息。使用這些信息,你可以創建出一個完整的應用程序,使用戶的體驗更加流暢。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/155408.html