本篇文章給大家分享的是有關(guān)如何在Html5中獲取高德地圖定位天氣,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
創(chuàng)新互聯(lián)建站于2013年開(kāi)始,先為濟(jì)源等服務(wù)建站,濟(jì)源等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為濟(jì)源企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。創(chuàng)建一個(gè)AMap.js文件
// AMap.js // 高德map /tupian/20230522/maps 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 = '/tupian/20230522/maps 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è)置定位超時(shí)時(shí)間,默認(rèn):無(wú)窮大 timeout: 10000, // 定位按鈕的停靠位置的偏移量,默認(rèn):Pixel(10, 20) buttonOffset: new AMap.Pixel(10, 20), // 定位成功后調(diào)整地圖視野范圍使定位位置及精度范圍視野內(nèi)可見(jiàn),默認(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í)時(shí)氣溫,單位:攝氏度 weather: "陰", //天氣預(yù)報(bào) windDirection: "東", // 風(fēng)向,風(fēng)向編碼對(duì)應(yīng)描述 windPower: "≤3", //風(fēng)力,風(fēng)力編碼對(duì)應(yīng)風(fēng)力級(jí)別,單位:級(jí) } 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) { // 定位出錯(cuò) 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中獲取高德地圖定位天氣,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:如何在Html5中獲取高德地圖定位天氣-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://chinadenli.net/article16/cdjjdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、搜索引擎優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、ChatGPT、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容