高德地图渲染问题

发布于 2022-09-11 23:48:21 字数 2630 浏览 26 评论 0

地图渲染问题
1.能得到对应的经纬度和二维数组
2.能打印出GDMap不为null
3.渲染报错为:Uncaught TypeError: Cannot read property 'GDMap' of undefined

<template>
  <div class="map">
    <div id="GDMap" v-loading="loading"></div>
  </div>
</template>

<script>
import loadMap from '@/api/loadMap'
export default {
  data () {
    return {
      // 地图实例
      GDMap: null,
      // 加载的一些插件
      plugins: [
        'AMap.OverView',
        'AMap.MouseTool',
        'AMap.PolyEditor',
        'AMap.RectangleEditor',
        'AMap.PlaceSearch',
        'AMap.DistrictLayer',
        'AMap.CustomLayer',
        'AMap.DistrictSearch',
        'AMap.Polygon',
        'AMap.OverlayGroup'
      ],
      key: 'c5eac55551560531336988396dacbf53',
      v: '1.4.14',
      loading: true,
    }
  },
 mounted () {
    loadMap(this.key, this.plugins, this.v)
      .then(AMap => {
        this.GDMap = new AMap.Map('GDMap', {
          zoom: 6,
          center: [116.397428, 39.90923],
          mapStyle: 'amap://styles/dark',
          resizeEnable: true, //监控地图容器尺寸变化
        })
        this.GDMap.on('complete', () => {
          // eslint-disable-next-line no-console
          console.log('地图加载成功')
          this.loading = false
        })
        new AMap.DistrictSearch({
          extensions: 'all',
          subdistrict: 0
        }).search('北京市', function (status, result) {
          var outer = [
            new AMap.LngLat(-360, 90, true),
            new AMap.LngLat(-360, -90, true),
            new AMap.LngLat(360, -90, true),
            new AMap.LngLat(360, 90, true),
          ];
          // 完整行政区边界坐标点
          var holes = result.districtList[0].boundaries
          var pathArray = [
            outer
          ];
          pathArray.push.apply(pathArray, holes)
          var polygon = new AMap.Polygon({
            pathL: pathArray,
            strokeColor: '#00eeff',
            strokeWeight: 1,
            fillColor: '#71B3ff',
            fillOpacity: 0.5
          });
          polygon.setPath(pathArray);

      // eslint-disable-next-line no-console
      console.log(pathArray, "--PATH--")

      // 渲染地图行政区轮廓 ? 有问题
      setTimeout(() => {
        this.GDMap.add(polygon)
      }, 10000)
    })
    // eslint-disable-next-line no-console
    console.log(this.GDMap, '--GDMap--')

  })
  .catch(() => {
    // eslint-disable-next-line no-console
    console.log('地图加载失败!')
    this.loading = false
  })
},
  methods: {

  },
}
</script>

<style>
#GDMap {
  width: 1200px;
  height: 500px;
  position: relative;
}
</style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找个人就嫁了吧 2022-09-18 23:48:21

this指向问题,把search('北京市', function (status, result)这行的这个函数改为箭头函数

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文