外观
定位和天气接口
约 481 字大约 2 分钟
2025-09-16
MacOS 获取定位
brew install corelocationcli
corelocationcli
高德开放平台
https://console.amap.com/dev/index
获取准确定位
curl -s ipinfo.io/geo
{
"ip": "111.28.244.26",
"city": "Xiuying",
"region": "Hainan",
"country": "CN",
"loc": "20.0007,110.2936",
"org": "AS9808 China Mobile Communications Group Co., Ltd.",
"postal": "570000",
"timezone": "Asia/Shanghai",
"readme": "https://ipinfo.io/missingauth"
}
# 返回 "维度,经度"
curl -s ipinfo.io/geo | jq '.loc'
"20.0007,110.2936"
高德地图 API 获取位置信息
curl -s "https://restapi.amap.com/v3/geocode/regeo?key=<api_key>&location=110.2936,20.0007"
{"status":"1","regeocode":{"addressComponent":{"city":"海口市","province":"海南省","adcode":"460105","district":"秀英区","towncode":"460105001000","streetNumber":{"number":"68号","location":"110.293292,20.000087","direction":"西南","distance":"75.3616","street":"丘海大道"},"country":"中国","township":"秀英街道","seaArea":"南海","businessAreas":[{"location":"110.309984,20.017143","name":"海秀","id":"460106"},{"location":"110.307559,20.005070","name":"海垦","id":"460106"}],"building":{"name":[],"type":[]},"neighborhood":{"name":[],"type":[]},"citycode":"0898"},"formatted_address":"海南省海口市秀英区秀英街道海南森林音乐艺术培训中心铂金华府"},"info":"OK","infocode":"10000"}
高德地图 API 获取天气信息
curl -s "https://restapi.amap.com/v3/weather/weatherInfo?city=<adcode>&key=<api_key>&extensions=base"
{"status":"1","count":"1","info":"OK","infocode":"10000","lives":[{"province":"海南","city":"秀英区","adcode":"460105","weather":"小雨","temperature":"28","winddirection":"西","windpower":"5","humidity":"71","reporttime":"2025-09-24 15:04:25","temperature_float":"28.0","humidity_float":"71.0"}]}
emacs-lisp 实现
(defvar my-amap-api-key "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"高德地图 API key")
(defun shell-command-plist (command)
(json-parse-string (shell-command-to-string command)
:object-type 'plist))
(defun my-ip-info ()
(shell-command-plist "curl -s ipinfo.io/geo"))
(defun my-lon-lot ()
(pcase system-type
('darwin
(let* ((str (shell-command-to-string "corelocationcli"))
(lst (split-string (string-trim str) " ")))
(concat (cadr lst) "," (car lst))))
(_ (let* ((str (plist-get (my-ip-info) :loc))
(lst (split-string str ",")))
(concat (cadr lst) "," (car lst))))))
(defun my-location-info ()
(shell-command-plist
(format "curl -s \"https://restapi.amap.com/v3/geocode/regeo?key=%s&location=%s\"" my-amap-api-key (my-lon-lot))))
(defun my-city-info ()
(let ((loc (my-location-info)))
(plist-get (plist-get loc :regeocode)
:addressComponent)))
(defun my-city-adcode ()
(plist-get (my-city-info) :adcode))
(defun my-city-name ()
(plist-get (my-city-info) :city))
(defun my-weather-info ()
(shell-command-plist
(format "curl -s \"https://restapi.amap.com/v3/weather/weatherInfo?city=%s&key=%s&extensions=base\"" (my-city-adcode) my-amap-api-key)))
(defun my-weather ()
(plist-get (aref (plist-get (my-weather-info) :lives) 0)
:weather))
(defun my-temperature ()
(plist-get (aref (plist-get (my-weather-info) :lives) 0)
:temperature))