溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在Html5如何獲取高德地圖定位的天氣

發(fā)布時間:2022-02-22 14:31:58 來源:億速云 閱讀:183 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下在Html5如何獲取高德地圖定位的天氣,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

注:使用的是的模塊注入方式,適用各種前端單頁面應(yīng)用及H5

創(chuàng)建一個AMap.js文件:

// AMap.js

// 高德map   https://webapi.amap.com/maps?v=1.4.11&key=你的高德地圖的key
export default function MapLoader () {
return new Promise((resolve, reject) => {
if (window.AMap) {
  resolve(window.AMap)
} else {
  var script = document.createElement('script')
  script.type = 'text/javascript'
  script.async = true
  //這里引入的是全部模塊,或者按需要模塊引入,加參數(shù)plugin=“模塊名”
  script.src =
    'http://webapi.amap.com/maps?v=1.4.11&callback=initAMap&key=6747cb97****************7e774b4b62' //你的高德應(yīng)用AK (申請參考官方文檔)
  script.onerror = reject
  document.head.appendChild(script)''
}
window.initAMap = () => {
  resolve(window.AMap)
}
})
}

使用

vue 示例:

import MapLoader from '@/common/SDK/AMap.js'

MapLoader().then(AMap => {
                //加載定位插件
                AMap.plugin(['AMap.Geolocation', 'AMap.Weather'], function() {
                    var geolocation = new AMap.Geolocation({
                        // 是否使用高精度定位,默認(rèn):true
                        enableHighAccuracy: true,
                        // 設(shè)置定位超時時間,默認(rèn):無窮大
                        timeout: 10000,
                        // 定位按鈕的??课恢玫钠屏浚J(rèn):Pixel(10, 20)
                        buttonOffset: new AMap.Pixel(10, 20),
                        //  定位成功后調(diào)整地圖視野范圍使定位位置及精度范圍視野內(nèi)可見,默認(rèn):false
                        zoomToAccuracy: true,
                        //  定位按鈕的排放位置,  RB表示右下
                        buttonPosition: 'RB'
                    })
            
                    geolocation.getCurrentPosition()
                    AMap.event.addListener(geolocation, 'complete', onComplete)
                    AMap.event.addListener(geolocation, 'error', onError)
                    var weather = new AMap.Weather();
            
                    function onComplete(data) {
                        // data是具體的定位信息
                        that.$store.dispatch('UPDATE_ADDRESS', data.formattedAddress)
                        // weather.getForecast(data.addressComponent.adcode, function(err, data) {
                        //     console.log(err, data);
                        // });
                        weather.getLive(data.addressComponent.adcode, function(err, data) {
                            // console.log(err, data);
                            let obj = {
                                adcode: "330100", //區(qū)域編碼
                                city: "杭州市", //城市
                                humidity: "92", //空氣濕度(百分比)
                                info: "OK", //狀態(tài)
                                province: "浙江", //省份
                                reportTime: "2019-12-24 19:55:48",
                                temperature: 10, //實(shí)時氣溫,單位:攝氏度
                                weather: "陰", //天氣預(yù)報
                                windDirection: "東", // 風(fēng)向,風(fēng)向編碼對應(yīng)描述
                                windPower: "≤3", //風(fēng)力,風(fēng)力編碼對應(yīng)風(fēng)力級別,單位:級
                            }
                            let weatherObj = {
                                date: `${that.$moment().format('MM月DD日')}`,
                                week: `${that.$moment().format('d')}`,
                                temperature: data.temperature,
                                currentCity: data.city,
                                weatherDesc: data.weather
                            }
                            that.$store.dispatch("UPDATE_Weather", weatherObj)
                        });
            
                    }
            
                    function onError(data) {
                        // 定位出錯
                        if (data.info == 'NOT_SUPPORTED') {
                            uni.showModal({
                                title: '提示',
                                content: '當(dāng)前瀏覽器不支持定位功能' || '定位失敗'
                            })
                        } else if (data.info == 'FAILED') {
                            uni.showModal({
                                title: '提示',
                                content: data.message || '定位失敗'
                            })
                        }
            
                    }
                })
            }, e => {
                console.log('地圖加載失敗', e)
            })
        }

看完了這篇文章,相信你對“在Html5如何獲取高德地圖定位的天氣”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI