初體驗
最近接觸到了boxjs,看到了里面一個比較有意思的彩雲天氣的腳本,由於自己本身就是彩雲天氣pro的用戶,日常使用過程中感覺到彩雲的降雨提醒還是挺方便的,於是就准備開始使用這個天氣的腳本。
腳本主要用到兩個平台的接口:
測試環境選擇了自己的iphone上的JSbox
來運行一個簡單的js腳本:
//簡單思路就是 獲取ip再獲取天氣信息
const locationKey = "XXXXXXXXXXXXX"
const weatherKey = "XXXXXXXXXXXX"
const apiList = {
location:"https://apis.map.qq.com/ws"
}
function getLonLat(){
$http.get({
url: `${apiList.location}/location/v1/ip?key=${locationKey}`,
handler: (resp) => {
let location = resp.data&&resp.data.result&&resp.data.result.location
getLocation(location)
}
});
}
function getLocation(location){
$http.get({
url: `${apiList.location}/geocoder/v1/?key=${locationKey}&location=${location.lat},${location.lng}`,
handler: (resp) => {
var data = resp.data;
$console.info(data.result.formatted_addresses.recommend);
}
});
}
/**
*
* @param {lat:"",lng:""} location
*/
function getWeather(location){
$http.get({
url: `${apiList.weather}/${weatherKey}/${location.lng},${location.lat}/weather.json`,
handler: (resp) => {
let data = resp.data;
console.info(data)
//運行結果參照彩雲天氣https://open.caiyunapp.com/%E9%80%9A%E7%94%A8%E9%A2%84%E6%8A%A5%E6%8E%A5%E5%8F%A3/v2.5
}
});
}
getLonLat()
頓時醒悟
寫到這其實我只是想測試一下兩個接口的基本用法以及可用之處,然后突然想到jsbox里面內置的$location
可以直接獲取到設備的位置信息,通過這樣獲取到的位置坐標會比ip的更加精准
//根據原生SDK獲取手機位置
function getPhoneLoc(){
$location.fetch({
handler: function(resp) {
var lat = resp.lat;
var lng = resp.lng;
var alt = resp.alt;
let loc = {lat:lat,lng:lng}
getLocation(loc)
}
});
}
最后運行結果
各位晚安