一、地理位置
你所在的经纬度是{{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/n/155408.html