封装高德地图

封装:

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

script.src = 'http://webapi.amap.com/maps?v=1.4.14&callback=initAMap&key=ee80c1540f06a00d5a574ca6b4974422'

script.onerror = reject

document.head.appendChild(script)

}

window.initAMap = () => {

resolve(window.AMap)

}

})

}

 

 

 

 

使用方法:

let that = this;

MapLoader().then(

AMap => {

let map = new AMap.Map("container", {

resizeEnable: true

});

map.plugin("AMap.Geolocation", function() {

let geolocation = new AMap.Geolocation({

enableHighAccuracy: true, //是否使用高精度定位,默认:true

timeout: 10000, //超过10秒后停止定位,默认:5s

buttonPosition: "RB", //定位按钮的停靠位置

buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)

zoomToAccuracy: true //定位成功后是否自动调整地图视野到定位点

});

map.addControl(geolocation);

geolocation.getCurrentPosition(function(status, result) {

if (status == "complete") {

//that.address = result.formattedAddress;

that.latitude = result.position.lat;

that.longitude = result.position.lng;

that.load(that.latitude, that.longitude);

} else {

}

});

});

},

e => {}

);

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值