校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > Html5獲取高德地圖定位天氣的方法

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

熱門標簽:惡搞電話機器人 黃石ai電銷機器人呼叫中心 地圖標注軟件打印出來 欣鼎電銷機器人 效果 智能電銷機器人被禁用了么 高德地圖標注商戶怎么標 如何查看地圖標注 電話機器人技術 ok電銷機器人

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

創建一個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
  //這里引入的是全部模塊,或者按需要模塊引入,加參數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)
            })
        }

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

標簽:綏化 聊城 赤峰 金昌 中山 阿壩 萍鄉 盤錦

巨人網絡通訊聲明:本文標題《Html5獲取高德地圖定位天氣的方法》,本文關鍵詞  Html5,獲取,高德,地圖,定位,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Html5獲取高德地圖定位天氣的方法》相關的同類信息!
  • 本頁收集關于Html5獲取高德地圖定位天氣的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 漯河市| 大石桥市| 乌鲁木齐市| 巨野县| 丹凤县| 南溪县| 垣曲县| 泰安市| 大庆市| 宜宾县| 大冶市| 方城县| 新兴县| 永丰县| 碌曲县| 光山县| 德兴市| 阜康市| 鹰潭市| 申扎县| 乌审旗| 重庆市| 井陉县| 景德镇市| 固原市| 方正县| 陈巴尔虎旗| 柞水县| 年辖:市辖区| 昌黎县| 石泉县| 德江县| 长宁区| 收藏| 前郭尔| 惠来县| 吴川市| 阿拉尔市| 扶沟县| 潮安县| 通化市|