vue调用高德可能会报错Error: Invalid Object: Pixel(NaN, NaN)

文章讲述了在Vue项目中集成高德地图遇到的问题,主要是因为地图容器样式未设置导致数据错误。通过添加正确的CSS样式并确保数据源正确,作者成功解决了这个问题并分享了解决方案。
摘要由CSDN通过智能技术生成

感谢高德小哥哥的在线解答服务太棒了,虽然是已经解决了问题然后才看见小哥哥回复了,但是还是很棒哈!!!

叙述如下:

这种情况Gpt说的是可能数据不对,但是我用的是高德官方的数据直接拿过来弄的,而且我每个数据都输出打印了。数据是没有问题的然后就陷入死循环:

但是昂,关键的时刻来了:

<template>

  <div ref="container" id="container"></div>

</template>

这个代码在写地图div容器的时候 我没有给这个div设置style

这里是空的,然后就会报上面那个错误,给了下面这样的之后完美显示出来;

#container {

width: 100%;

height: 950px;

position: fixed;

top: 0;

left: 0;

z-index: 1; /* Ensure map is above normal content but below fixed positioned button */

}

这就完美解决报错。所以在用高德地图的时候这个数据报错,如果直接用的人家的代码基础上修改的话,数据引用的一般都是在线数据所以数据可能不会出错。反正注意好吧。这个报错可能是你地图容器什么的没有弄好。

看csdn上没有vue写这个高德的这个我就把这个样例代码也给了:

js文件名字Heatmap.js

import AMapLoader from '@amap/amap-jsapi-loader';
import {ref} from "vue";
let map = ref(null);
let loca = ref(null);
export function inithotmap(){
    window._AMapSecurityConfig = {
        securityJsCode:'7331a8d50a64ee1212f2ca1c90d3b8de',//安全密钥
    }
    AMapLoader.load({
        key: "312e6012a76e8f5690b25cdc0ca025a6",
        version: "2.0",
        Loca:{
            version: "2.0.0",
        }
      }).then(AMap =>{
        map = new AMap.Map("container", {
            viewMode: "3D",
            zoom: 11,
            center: [113.664206,34.737714],
            mapStyle: "amap://styles/light"
          });
          console.log(Loca)
        loca = new Loca.Container({
            map:map,
        });

        var geo = new Loca.GeoJSONSource({
            // data: [],
            url: 'https://a.amap.com/Loca/static/loca-v2/demos/mock_data/china_traffic_event.json',
        });

        var scatter = window.ll = new Loca.ScatterLayer({
            // loca,
            zIndex: 10,
            opacity: 1,
            visible: true,
            zooms: [2, 22],
        });
        scatter.setSource(geo, {
            unit: 'px',
            size: [20, 20],
            texture: 'https://a.amap.com/Loca/static/loca-v2/demos/images/blue.png',
            borderWidth: 0,
        });
        console.log(scatter)
        loca.add(scatter);


        // 呼吸
      var top10 = {
        type: 'FeatureCollection',
        features: [
            {
                "type": "Feature",
                "properties": {
                    "cityName": "韶关市",
                    "ratio": 0,
                    "rank": 96
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        113.58052,
                        24.760098
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "cityName": "乐山市",
                    "ratio": 0,
                    "rank": 97
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        103.75082,
                        29.58099
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "cityName": "阜阳市",
                    "ratio": 0,
                    "rank": 98
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        115.82654,
                        32.889915
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "cityName": "荆门市",
                    "ratio": 0,
                    "rank": 99
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        112.209816,
                        30.997377
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "cityName": "哈尔滨市",
                    "ratio": 0,
                    "rank": 100
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        126.61314,
                        45.746685
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "cityName": "达州市",
                    "ratio": 0,
                    "rank": 101
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        107.493,
                        31.205515
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "cityName": "自贡市",
                    "ratio": 0,
                    "rank": 102
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        104.777824,
                        29.34555
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "cityName": "陇南市",
                    "ratio": 0,
                    "rank": 103
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        104.93356,
                        33.388184
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "cityName": "南充市",
                    "ratio": 0,
                    "rank": 104
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        106.1188,
                        30.800997
                    ]
                }
            },
            {
                "type": "Feature",
                "properties": {
                    "cityName": "恩施土家族苗族自治州",
                    "ratio": 0,
                    "rank": 105
                },
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        109.48512,
                        30.298103
                    ]
                }
            }
        ]
        
    };

    var breath = new Loca.ScatterLayer({
        zIndex: 121,
    });
    breath.setSource(new Loca.GeoJSONSource({
        data: top10,
    }));
    breath.setStyle({
        unit: 'px',
        size: [50, 50],
        texture: 'https://a.amap.com/Loca/static/loca-v2/demos/images/breath_red.png',
        animate: true,
        duration: 1000,
    });
    console.log(breath)
    loca.add(breath);
    loca.animate.start();

    var dat = new Loca.Dat();
    dat.addLayer(breath, '呼吸点');
    dat.addLayer(scatter, '蓝色气泡');
})
}

然后vue页面代码;这个就是一个简单的demo

<template>
     <div id="container"></div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { inithotmap } from '@/services/Heatmap';
import { onMounted, onUnmounted } from "vue";
let map = null;
onMounted(()=>{
  inithotmap();
})
onUnmounted(() => {
map?.destroy();
});

</script>

<style scoped>
#container {
width: 100%;
height: 950px;
position: fixed;
top: 0;
left: 0;
z-index: 1; /* Ensure map is above normal content but below fixed positioned button */
}
</style>

上面是组件式的写法,然后响应化LOCA这些实例好像不太好然后会导致实例还没加载完你可能用的时候就会报错,下面附上高德工作人员给的选项式写的代码,感觉维护度高一些,很不错,希望官方文档把之前的JavaScript也给与vue的文档这样会好些

<template>
  <div class="home_div">
    <div id="container"></div>
  </div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";

export default {
  name: "Mapview",
  data() {
    return {
    };
  },
  created() {},
  mounted() {
    this.initAMap();
  },
  methods: {
    initAMap() {
      AMapLoader.load({
        key: "312e6012a76e8f5690b25cdc0ca025a6", 
        version: "2.0",
        plugins: ["AMap.ToolBar", "AMap.Driving"], 
        Loca: {
          version: "2.0.0" 
        }
      })
        .then(AMap => {
          var map = new AMap.Map("container", {
            viewMode: "3D",
            zoom: 11,
            center: [113.664206, 34.737714],
            mapStyle: "amap://styles/light"
          });
          console.log(map);
         
          var loca = new Loca.Container({
            map: map
          });
       

          var geo = new Loca.GeoJSONSource({
            // data: [],
            url:
              "https://a.amap.com/Loca/static/loca-v2/demos/mock_data/china_traffic_event.json"
          });

          var scatter = (window.ll = new Loca.ScatterLayer({
            // loca,
            zIndex: 10,
            opacity: 1,
            visible: true,
            zooms: [2, 22]
          }));
          scatter.setSource(geo, {
            unit: "px",
            size: [20, 20],
            texture:
              "https://a.amap.com/Loca/static/loca-v2/demos/images/blue.png",
            borderWidth: 0
          });
          console.log(scatter);
          loca.add(scatter);

          // 呼吸
          var top10 = {
            type: "FeatureCollection",
            features: [
              {
                type: "Feature",
                properties: {
                  cityName: "韶关市",
                  ratio: 0,
                  rank: 96
                },
                geometry: {
                  type: "Point",
                  coordinates: [113.58052, 24.760098]
                }
              },
              {
                type: "Feature",
                properties: {
                  cityName: "乐山市",
                  ratio: 0,
                  rank: 97
                },
                geometry: {
                  type: "Point",
                  coordinates: [103.75082, 29.58099]
                }
              },
              {
                type: "Feature",
                properties: {
                  cityName: "阜阳市",
                  ratio: 0,
                  rank: 98
                },
                geometry: {
                  type: "Point",
                  coordinates: [115.82654, 32.889915]
                }
              },
              {
                type: "Feature",
                properties: {
                  cityName: "荆门市",
                  ratio: 0,
                  rank: 99
                },
                geometry: {
                  type: "Point",
                  coordinates: [112.209816, 30.997377]
                }
              },
              {
                type: "Feature",
                properties: {
                  cityName: "哈尔滨市",
                  ratio: 0,
                  rank: 100
                },
                geometry: {
                  type: "Point",
                  coordinates: [126.61314, 45.746685]
                }
              },
              {
                type: "Feature",
                properties: {
                  cityName: "达州市",
                  ratio: 0,
                  rank: 101
                },
                geometry: {
                  type: "Point",
                  coordinates: [107.493, 31.205515]
                }
              },
              {
                type: "Feature",
                properties: {
                  cityName: "自贡市",
                  ratio: 0,
                  rank: 102
                },
                geometry: {
                  type: "Point",
                  coordinates: [104.777824, 29.34555]
                }
              },
              {
                type: "Feature",
                properties: {
                  cityName: "陇南市",
                  ratio: 0,
                  rank: 103
                },
                geometry: {
                  type: "Point",
                  coordinates: [104.93356, 33.388184]
                }
              },
              {
                type: "Feature",
                properties: {
                  cityName: "南充市",
                  ratio: 0,
                  rank: 104
                },
                geometry: {
                  type: "Point",
                  coordinates: [106.1188, 30.800997]
                }
              },
              {
                type: "Feature",
                properties: {
                  cityName: "恩施土家族苗族自治州",
                  ratio: 0,
                  rank: 105
                },
                geometry: {
                  type: "Point",
                  coordinates: [109.48512, 30.298103]
                }
              }
            ]
          };

          var breath = new Loca.ScatterLayer({
            zIndex: 121
          });
          breath.setSource(
            new Loca.GeoJSONSource({
              data: top10
            })
          );
          breath.setStyle({
            unit: "px",
            size: [50, 50],
            texture:
              "https://a.amap.com/Loca/static/loca-v2/demos/images/breath_red.png",
            animate: true,
            duration: 1000
          });
          console.log(breath);
          loca.add(breath);
          loca.animate.start();

          var dat = new Loca.Dat();
          dat.addLayer(breath, "呼吸点");
          dat.addLayer(scatter, "蓝色气泡");

         
        })
        .catch(e => {
          console.log(e);
        });
    }
  }
};
</script>
<style  scoped>
.home_div {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 100%;
  position: relative;
}

#container {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 100%;
  position: absolute;
}


</style>

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值