在Html5如何获取高德地图定位的天气

发布时间:2022-02-22 14:31:58 作者:小新
来源:亿速云 阅读:192

小编给大家分享一下在Html5如何获取高德地图定位的天气,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

注:使用的是的模块注入方式,适用各种前端单页面应用及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如何获取高德地图定位的天气”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 高德地图----根据城市位置查询出当天的天气
  2. vue怎么使用高德地图根据坐标定位点

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

html5

上一篇:在html5中如何使用Canvas帧动画吃苹果小游戏

下一篇:怎么用html5实现小程序商品飞入购物车

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》