javascript - vue.js mapbox map 未显示

标签 javascript vue.js mapbox

我想要显示我的 map ,但无论我做什么,它都不会显示 map 。

我当前使用的代码如下:

<template>
    <div>
        <MglMap
            :accessToken="mapboxAccessToken"
            :mapStyle.sync="mapStyle"
            :center="coordinates"
        />

        <button class="btn btn-contained btn-success add-location" v-on:click="addLocation"><span>Button</span></button>
    </div>

</template>

<script>
    import Mapbox from "mapbox-gl";
    import { MglMap } from "vue-mapbox";

    export default {
        components: {
            MglMap
        },
        props: {
            currLat: Number,
            currLon: Number,
            allPoints: Array
        },
        // Mounted (page load), we ask to track
        mounted: function () {
            const options = {
                enableHighAccuracy: true
            };
            this.$watchLocation(options)
                .then(coordinates => {
                    // Set the center to my coordinates
                    this.currLat = coordinates.lat;
                    this.currLon = coordinates.lng;
                    // Emit the user data to all connected devices.
                    this.$socket.emit('USER_COORDS', {
                        'user': this.$socket.id,
                        'lat': this.currLat,
                        'lon': this.currLon
                    });
                });
        },
        data() {
            return {
                mapboxAccessToken: "removed",
                mapStyle: "mapbox://styles/mapbox/streets-v10",
                coordinates: [0.0, 0.0]
            };
        },
        created() {
            // We need to set mapbox-gl library here in order to use it in template
            this.map = Mapbox;
        },
        methods: {
            geolocateError(control, positionError) {
                // console.log(positionError);
                Latte.ui.notification.create({
                    title: "An error occurred!",
                    message: "We could not get your location, please try again by reloading the application."
                });
            },
            geolocate(control, position) {
                console.log(
                    `User position: ${position.coords.latitude}, ${position.coords.longitude}`
                )
            },
            addLocation: function() {
                this.$socket.emit('NEW_LOCATION', {
                    name: 'VK1',
                    lat: this.currLat,
                    lon: this.currLon
                });
            }
        },
        sockets: {
            USER_COORDS_DATA: function (data) {
                // user connects, data of the current location gets emitted
                // this.map.flyTo({
                //     center: [data.lon, data.lat],
                //     zoom: 15,
                //     speed: 1
                // });
                // console.log(data)
            },
            NEW_LOCATION_DATA: function (data) {
                // Returned data from the server (after storage)
                console.log(data)
            },
        },
    }
</script>

<style>
    @import '../../node_modules/mapbox-gl/dist/mapbox-gl.css';
    #map {
        width: 100%;
        height: calc(100vh - var(--app-bar-height));
    }
    .btn.add-location {
        position: absolute;
        left: 2rem;
        bottom: 2rem;
    }
</style>

我在控制台中得到以下输出:

console output

我不知道这里出了什么问题,甚至不知道为什么我会遇到 dom 异常?

我只想显示我的 map 并在您的位置上显示一个标记,知道这里发生了什么吗?为什么 map 没有渲染?

使用的包:
- "mapbox-gl": "^1.3.1",
- “vue-mapbox”:“^0.4.1”,

最佳答案

我也遇到了类似的问题,我无法完全显示 map ,甚至无法显示 VueMapbox 的演示 https://soal.github.io/vue-mapbox/不适合我。

但是,我在 chrome 中检查后发现整个 div 的高度设置为 0,这很奇怪。然后我手动设置高度,它起作用了 enter image description here

我在项目 github 上创建了一个问题,所以我们可能会看到问题出在哪里:https://github.com/soal/vue-mapbox/issues/158

TLDR:手动设置高度

关于javascript - vue.js mapbox map 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58048819/

相关文章:

javascript - 隐藏所有网站的div

javascript - 当你有多个时验证单个 Prop

javascript - Apexchart 工具提示

java - 为什么动态添加的符号在 Mapbox 中显示为灰色

javascript - 从动态生成的表单中捕获用户数据 (Jquery-Form-Builder.js)

javascript - 为什么我的 fancybox 功能不起作用?

javascript - 如何在谷歌地图信息窗口中放置 html div

vue.js - 如何使用testcafe测试 Electron 应用程序的外部浏览器页面

javascript - 从mapbox API获取geoJSON数据以确定多边形中的点

ios - 如何使用 Mapbox-iOS-SDK 确定 mapview 缩放级别何时更改?