vue中封装腾讯地图组件 - Go语言中文社区

vue中封装腾讯地图组件


vue中封装腾讯地图

直接上代码

export function TMap (key) {
  return new Promise(function (resolve, reject) {
    window.init = function () {
      resolve(window.qq)// 注意这里
    }
    var script = document.createElement('script')
    script.type = 'text/javascript'
    script.src = 'https://map.qq.com/api/js?v=2.exp&callback=init&key=这写自己申请的key'
    script.onerror = reject
    document.head.appendChild(script)
  })
}

页面调用

<div id="container" style="width:1070px;height:450px;"></div>
import { TMap } from '../utils/TXmap'
TMap () {
      TMap().then(qq => {
        var center = new qq.maps.LatLng(this.datatable[0].workon_latitude, this.datatable[0].workon_longitude)
        var map = new qq.maps.Map(document.getElementById('container'), {
          // 地图的中心地理坐标
          center: center,
          // 初始化地图缩放级别
          zoom: 11
        })
        var infoWin = new qq.maps.InfoWindow({
          map: map
        })
        // let latlngBounds = new qq.maps.LatLngBounds()
        console.log('latlngBounds')
        var thih = this
        var marker
        for (let i = 0; i < thih.datatable.length; i++) {
          marker = new qq.maps.Marker({
            position: new qq.maps.LatLng(thih.datatable[i].workon_latitude, thih.datatable[i].workon_longitude),
            map: map
          })
          marker.id = thih.datatable[i].station_id
          qq.maps.event.addListener(marker, 'click', function () {
            sessionStorage.setItem('particulars', JSON.stringify(thih.datatable[i]))
            console.log(thih.datatable[i])
            infoWin.open()
            // eslint-disable-next-line no-useless-escape
            infoWin.setContent(`<div>${thih.datatable[i].station_name} <p>人数: ${thih.datatable[i].workers_num} <button onclick='aaa("${thih.datatable[i].id}")' style='color:red'>查看详情</button></p></div>`)
            // 提示窗位置
            infoWin.setPosition(new qq.maps.LatLng(thih.datatable[i].workon_latitude, thih.datatable[i].workon_longitude))
          })
        }
      })
    }

这是我的页面效果,不懂可以私聊
在这里插入图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_41359437/article/details/105200557
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢