vue 图片拖动加载 类似于地图_vue加载高德地图,并自定义infoWindow事件

1. 加载高德地图

  • 公共方法
9d12368d3174116c9d5ee129ab5b3e0a.png
295a5f48a8d82211f75653a04bcd2f55.png
  • 具体加载方法
abf59e50930b7ba7c8a4e7fca630821f.png

2.自定义infoWindow事件

使用Vue.extend()生成html

8553ec448eda06956499ab19e456dc3f.png

源码如下:

/** * 创建script * @param url * @returns {Promise} */const createScript = (url, hasCallback) => { let scriptElement = document.createElement('script') document.body.appendChild(scriptElement) let promise = new Promise((resolve, reject) => { scriptElement.addEventListener('load', e => { removeScript(scriptElement) if (!hasCallback) { resolve(e) } }, false) scriptElement.addEventListener('error', e => { removeScript(scriptElement) reject(e) }, false) if (hasCallback) { window.____callback____ = function () { resolve() window.____callback____ = null } } }) if (hasCallback) { url += '&callback=____callback____' } scriptElement.src = url return promise}/** * 移除script标签 * @param scriptElement script dom */const removeScript = (scriptElement) => { document.body.removeChild(scriptElement)}
/** * 加载高德地图 */export function mapLoader (keyType) { const key = keyType ? apiKey[keyType] : apiKey.web return new Promise((resolve, reject) => { if (window.AMap) { resolve(window.AMap) } else { createScript(`https://webapi.amap.com/maps?v=1.4.14&callback=initAMap&key=${key}`) } window.initAMap = () => { resolve(window.AMap) } })}
 marker = new AMap.Marker({ map: that.map, position: [item.lon, item.lat], icon: new AMap.Icon({ size: new AMap.Size(19, 25), image: require('@/assets/images/img.png'), imageSize: new AMap.Size(19, 25) }) }) // 弹窗点击跳转详情 Popup = Vue.extend({ render (createElement) { return createElement('div', { domProps: { innerHTML: item.name }, on: { click: () => { that.$router.push({ name: 'home' }) } } }) } }) marker.content = (new Popup()).$mount('').$el marker.on('click', e => { infoWindow.setContent(e.target.content) infoWindow.open(that.map, e.target.getPosition()) })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值