国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > HTML5 > 正文

Html5獲取高德地圖定位天氣的方法

2024-09-05 07:23:12
字體:
來源:轉載
供稿:網友

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

創建一個AMap.js文件

// AMap.js// 高德map   https://webapi.amap.com/maps?v=1.4.11&key=你的高德地圖的keyexport 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  //這里引入的是全部模塊,或者按需要模塊引入,加參數plugin=“模塊名”  script.src =    'http://webapi.amap.com/maps?v=1.4.11&callback=initAMap&key=6747cb97****************7e774b4b62' //你的高德應用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({                        // 是否使用高精度定位,默認:true                        enableHighAccuracy: true,                        // 設置定位超時時間,默認:無窮大                        timeout: 10000,                        // 定位按鈕的停靠位置的偏移量,默認:Pixel(10, 20)                        buttonOffset: new AMap.Pixel(10, 20),                        //  定位成功后調整地圖視野范圍使定位位置及精度范圍視野內可見,默認: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", //區域編碼                                city: "杭州市", //城市                                humidity: "92", //空氣濕度(百分比)                                info: "OK", //狀態                                province: "浙江", //省份                                reportTime: "2019-12-24 19:55:48",                                temperature: 10, //實時氣溫,單位:攝氏度                                weather: "陰", //天氣預報                                windDirection: "東", // 風向,風向編碼對應描述                                windPower: "≤3", //風力,風力編碼對應風力級別,單位:級                            }                            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: '當前瀏覽器不支持定位功能' || '定位失敗'                            })                        } else if (data.info == 'FAILED') {                            uni.showModal({                                title: '提示',                                content: data.message || '定位失敗'                            })                        }                                }                })            }, e => {                console.log('地圖加載失敗', e)            })        }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宾川县| 米泉市| 屏东市| 通辽市| 万荣县| 砀山县| 华宁县| 洪泽县| 泸溪县| 镇坪县| 长海县| 安义县| 隆尧县| 永州市| 翁源县| 普宁市| 公主岭市| 东方市| 阳谷县| 方正县| 麦盖提县| 兰考县| 阳东县| 巧家县| 孙吴县| 马山县| 封丘县| 富宁县| 千阳县| 桃园县| 清水县| 襄垣县| 肥乡县| 东源县| 右玉县| 九江市| 湘乡市| 谷城县| 琼结县| 纳雍县| 临夏市|