Query real-time weather information using Amap (高德地圖) Weather API.
https://lbs.amap.com/api/javascript-api/guide/services/weather
Get Amap API Key
Set Environment Variable
export AMAP_API_KEY="your_api_key_here"
curl -X GET "https://restapi.amap.com/v3/weather/weatherInfo?city=BEIJING&key=YOUR_API_KEY&output=json"
curl -X GET "https://restapi.amap.com/v3/weather/weatherInfo?city=110000&key=YOUR_API_KEY&output=json"
curl -X GET "https://restapi.amap.com/v3/weather/weatherInfo?city=BEIJING|SHANGHAI|GUANGZHOU&key=YOUR_API_KEY&output=json"
curl -X GET "https://restapi.amap.com/v3/weather/weatherInfo?location=116.40,39.90&key=YOUR_API_KEY&output=json"
| Parameter | Type | Required | Description |
|---|---|---|---|
| city | String | Yes (or location) | City name or city code |
| location | String | Yes (or city) | Format: longitude,latitude |
| key | String | Yes | Amap API Key |
| output | String | No | Response format: json or xml (default: json) |
{ "status": "1", "infocode": "10000", "info": "Query Successful", "count": "1", "lives": [ { "province": "Beijing", "city": "Beijing", "adcode": "110000", "weather": "Clear", "temperature": "25", "winddirection": "North", "windpower": "2", "humidity": "45", "reporttime": "2024-01-15 12:00:00" } ] }小蔥技能站7w4.net每天更新,海量AI技能等你發現。
Create a weather query script:
#!/bin/bash
# weather.sh
API_KEY="${AMAP_API_KEY:-your_api_key_here}"
CITY="$1"
if [ -z "$CITY" ]; then
echo "Usage: weather.sh <city_name_or_code>"
exit 1
fi
curl -s "https://restapi.amap.com/v3/weather/weatherInfo?city=${CITY}&key=${API_KEY}&output=json" | jq '.'
Make it executable:
chmod +x weather.sh
./weather.sh BEIJING
| City | Code |
|---|---|
| Beijing | 110000 |
| Shanghai | 310000 |
| Guangzhou | 440100 |
| Shenzhen | 440300 |
| Hangzhou | 330100 |
| Chengdu | 510100 |
| Wuhan | 420100 |
| Xi'an | 610100 |