使用带有VUE 2的传单+ Mapbox图块导致错误试图读取LAT的错误

发布于 2025-02-11 13:29:41 字数 2995 浏览 1 评论 0原文

我有一个示例项目,位于 https://github.com/ericg-vue-vue-questions/传单检验

我正在关注用Vue和Flealet构建互动地图教程。

相关

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

<script>
import "leaflet/dist/leaflet.css";
import L from "leaflet";
//
// obtain a mapbox token from https://account.mapbox.com/access-tokens/
// and then create token.js in ./src/components with the following content:
//
//    const token = "<your token>";
//    export { token };
//
import {token} from "./token.js";

export default {
  name: "Map",

  data() {
    return{
      center: [37,7749, -122,4194]
    }
  },

  methods: {
    setupLeafletMap: function () {
      const mapDiv = L.map("mapContainer").setView(this.center, 13);
      L.tileLayer(
        `https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=${token}`,
        {
          attribution: 'Map data (c) <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery (c) <a href="https://www.mapbox.com/">Mapbox</a>',
          maxZoom: 18,
          id: "mapbox/streets-v11",
          accessToken: `${token}`,
        }
      ).addTo(mapDiv);
    },
  },

  mounted() {
    this.setupLeafletMap();
  },
};
</script>

<style scoped>
  #mapContainer {
    width: 500px;
    height: 500px;
  }
</style>

据我了解,这应该足以开始显示地图图块。

执行代码后,它会导致错误:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in mounted hook: "TypeError: Cannot read properties of null (reading 'lat')"

found in

---> <Map> at src/components/LeafletTest.vue
       <HelloWorld> at src/components/HelloWorld.vue
         <App> at src/App.vue
           <Root>

TypeError: Cannot read properties of null (reading 'lat')
    at Object.project (leaflet-src.js?e11e:1685:1)
    at Object.latLngToPoint (leaflet-src.js?e11e:1522:1)
    at NewClass.project (leaflet-src.js?e11e:3949:1)
    at NewClass._getNewPixelOrigin (leaflet-src.js?e11e:4472:1)
    at NewClass._move (leaflet-src.js?e11e:4191:1)
    at NewClass._resetView (leaflet-src.js?e11e:4153:1)
    at NewClass.setView (leaflet-src.js?e11e:3175:1)
    at VueComponent.setupLeafletMap (LeafletTest.vue?232a:30:1)
    at VueComponent.mounted (LeafletTest.vue?232a:44:1)
    at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1863:1)

我不确定出了什么问题或如何修复它。我需要更改什么,以便将地图图块加载。

I have a sample project at https://github.com/ericg-vue-questions/leaflet-test

I am following a Building an interactive map with Vue and Leaflet tutorial.

The relevant code is:

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

<script>
import "leaflet/dist/leaflet.css";
import L from "leaflet";
//
// obtain a mapbox token from https://account.mapbox.com/access-tokens/
// and then create token.js in ./src/components with the following content:
//
//    const token = "<your token>";
//    export { token };
//
import {token} from "./token.js";

export default {
  name: "Map",

  data() {
    return{
      center: [37,7749, -122,4194]
    }
  },

  methods: {
    setupLeafletMap: function () {
      const mapDiv = L.map("mapContainer").setView(this.center, 13);
      L.tileLayer(
        `https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=${token}`,
        {
          attribution: 'Map data (c) <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery (c) <a href="https://www.mapbox.com/">Mapbox</a>',
          maxZoom: 18,
          id: "mapbox/streets-v11",
          accessToken: `${token}`,
        }
      ).addTo(mapDiv);
    },
  },

  mounted() {
    this.setupLeafletMap();
  },
};
</script>

<style scoped>
  #mapContainer {
    width: 500px;
    height: 500px;
  }
</style>

As I understand it, this should be enough to start displaying map tiles.

When the code is executed, it results in the error:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in mounted hook: "TypeError: Cannot read properties of null (reading 'lat')"

found in

---> <Map> at src/components/LeafletTest.vue
       <HelloWorld> at src/components/HelloWorld.vue
         <App> at src/App.vue
           <Root>

TypeError: Cannot read properties of null (reading 'lat')
    at Object.project (leaflet-src.js?e11e:1685:1)
    at Object.latLngToPoint (leaflet-src.js?e11e:1522:1)
    at NewClass.project (leaflet-src.js?e11e:3949:1)
    at NewClass._getNewPixelOrigin (leaflet-src.js?e11e:4472:1)
    at NewClass._move (leaflet-src.js?e11e:4191:1)
    at NewClass._resetView (leaflet-src.js?e11e:4153:1)
    at NewClass.setView (leaflet-src.js?e11e:3175:1)
    at VueComponent.setupLeafletMap (LeafletTest.vue?232a:30:1)
    at VueComponent.mounted (LeafletTest.vue?232a:44:1)
    at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1863:1)

I am not sure what is going wrong or how to fix it. What do I need to change so the map tiles will be loaded.

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

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

发布评论

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

评论(1

赠佳期 2025-02-18 13:29:41

您必须将浮子用作长时间长。

喜欢:中心:[37.7749,-122.4194]

You have to use floats as lat long.

like: center: [37.7749, -122.4194]

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