1 #!/bin/bash 2 # 經緯度反解析地址區域,調用的是騰訊位置的api 3 4 parse_district(){ 5 url="https://apis.map.qq.com/ws/geocoder/v1/?location=$1,$2&key=4XCBZ-BPJ6G-23JQI-I2FZ3-ZSDQ7-E3BVp" 6 #echo ${url} 7 curl ${url} > index.json 8 # 使用正則提取須要的區域信息,實際有須要能夠提取省市等接口返回的其餘信息 9 grep -E '"district":(.*?),' index.json > district.info 10 res=`cat district.info` # eg: "district": "海淀區", 11 res=${res##*:} # "海淀區", 12 res=${res:0:-1} # "海淀區" 13 echo ${res} 14 } 15 16 # shell函數默認返回值是函數 打印的結果, 若是要使用return,只能是return int型數字 17 a1=`parse_district "$1" "$2"` 18 echo "這是處理返回後的結果: ${a1}"
上述是shell腳本, 好比文件名是: parse_district.shgit
執行的話傳入函數須要的兩個參數, 分別是latitude(緯度),和經度(longitude) eg: shell parse_district.sh 39.984154 116.307490shell
執行結果: "這是處理返回後的結果: 海淀區"json
ps: 上述key須要本身申請獲取;api