注:使用的是的模块注入方式,适用各种前端单页面应用及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)
            })
        }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。