javascript - 在 Vue/Laravel 中使用谷歌地图

标签 javascript vue.js vuejs2

尝试将 google map 实现到 Vue 组件中。但是过得很辛苦。实际上,没有错误。但也没有 map :) 好吧,我在下面尝试过的。

在 laravel blade 中我设置了我的 api。

<script async defer src="https://maps.googleapis.com/maps/api/js?key={{env('GOOGLE_MAPS_API')}}&callback=initMap"></script>

然后在Vue组件中;

data() {
        return {
            mapName: "map",
            //some other codes
        }           
},

mounted() {
        this.fetchEstates();
},
methods: {
        fetchEstates(page = 1) {
            axios.get('/ajax', {
                params: {
                    page
                }}).then((response) => {
                    // console.log(response);
                    this.estates = response.data.data;
                    //some other codes....
                    //some other codes....


},
computed: {

        //some other functions in computed... 
        //

        initMap: function(){
                    var options =
                        {
                            zoom : 6,
                            center : {
                                lat:34.652500,
                                lng:135.506302
                            }
                        };

                    var map = new google.maps.Map(document.getElementById(this.mapName), options);
                    var marker = new google.maps.Marker({
                        map: map,
                        icon: 'imgs/marker.png',
                        url: "/pages/estates.id",
                        label: {
                            text: this.estates.price,
                            color: "#fff",
                        },
                        position: {
                            lat: this.estates.lat,
                            lng: this.estates.lng
                        }
                    });
                        google.maps.event.addListener(marker, 'click', function () {
                            window.location.href = this.url;
                    });


                }
<div id="map"></div>

最后一个标记 url id bind 像这样在 Controller 中,

public function details($id)
{
    $estates = allestates::where('id', $id)->first();

    return view('pages.details', compact('estates'));

}

我是否遗漏了 Vue js 中的某些内容?谢谢!

最佳答案

根据我们在评论中的讨论,我了解到您的问题是因为 initMap() 执行时 this.estates 仍未定义。请记住,您正在使用异步操作(通过 axios)来填充 this.estates,因此它在运行时未定义。你可以做的是:

  1. 将 map 初始化逻辑保存在initMap()
  2. 移动所有 Google map 标记创建,直到 axios promise 得到解决。您可以将所有这些抽象为另一种方法,例如insertMarkers()

此外,请记住您需要在应用程序/组件数据中定义 estates,否则它不会响应。

这是一个例子:

data() {
    return {
        mapName: "map",

        // Create the estate object first, otherwise it will not be reactive
        estates: {}
    }           
},

mounted() {
    this.fetchEstates();
    this.initMap();
},
methods: {
    fetchEstates: function(page = 1) {
        axios.get('/ajax', {
            params: {
                page
            }}).then((response) => {
                this.estates = response.data.data;

                // Once estates have been populated, we can insert markers
                this.insertMarkers();

                //pagination and stuff... 
            });
    },

    // Iniitialize map without creating markers
    initMap: function(){
        var mapOptions =
            {
                zoom : 6,
                center : {
                    lat:34.652500,
                    lng:135.506302
                }
            };

        var map = new google.maps.Map(document.getElementById(this.mapName), mapOptions);
    },

    // Helper method to insert markers
    insertMarkers: function() {
        var marker = new google.maps.Marker({
            map: map,
            icon: 'imgs/marker.png',
            url: "/pages/estates.id",
            label: {
                text: this.estates.price,
                color: "#fff",
            },
            position: {
                lat: this.estates.lat,
                lng: this.estates.lng
            }
        });

        google.maps.event.addListener(marker, 'click', function () {
            window.location.href = this.url;
        });
    }
},

更新:还发现你没有解决this.estates的数据结构问题。看起来您正在从端点而不是对象接收一个数组,因此 this.estates 将返回一个数组,当然 this.estates.lat 将是未定义的。

如果您想遍历整个数组,则必须使用 this.estates.forEach() 在添加标记的同时遍历每个单独的庄园,即:

data() {
    return {
        mapName: "map",

        // Create the estate object first, otherwise it will not be reactive
        estates: {}
    }           
},

mounted() {
    this.fetchEstates();
    this.initMap();
},
methods: {
    fetchEstates: function(page = 1) {
        axios.get('/ajax', {
            params: {
                page
            }}).then((response) => {
                this.estates = response.data.data;

                // Once estates have been populated, we can insert markers
                this.insertMarkers();

                //pagination and stuff... 
            });
    },

    // Iniitialize map without creating markers
    initMap: function(){
        var mapOptions =
            {
                zoom : 6,
                center : {
                    lat:34.652500,
                    lng:135.506302
                }
            };

        var map = new google.maps.Map(document.getElementById(this.mapName), mapOptions);
    },

    // Helper method to insert markers
    insertMarkers: function() {

        // Iterate through each individual estate
        // Each estate will create a new marker
        this.estates.forEach(estate => {
            var marker = new google.maps.Marker({
                map: map,
                icon: 'imgs/marker.png',
                url: "/pages/estates.id",
                label: {
                    text: estate.price,
                    color: "#fff",
                },
                position: {
                    lat: estate.lat,
                    lng: estate.lng
                }
            });

            google.maps.event.addListener(marker, 'click', function () {
                window.location.href = this.url;
            });
        });
    }
},

关于javascript - 在 Vue/Laravel 中使用谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54479805/

相关文章:

javascript - 如何基于数组显示和设置列表样式

javascript - 如何从下面的文本中提取信息?

javascript - AXIOS之后更新静态吗? VUEJS

javascript - 从另一个 vuex 操作中调用 Vuex 操作

javascript - 如何使用 Vue 禁用表单提交时的 window.onbeforeunload ?

Javascript - 异步等待不适用于条件

javascript - Vue中如何修改 `App`组件的数据?

javascript - 有什么简写 JavaScript 可以让这个 reducer 变得更容易吗?

javascript - 为 Bootstrap Carousel 创建容器

vue.js - Vue.js : how to load a css animation only on the first page load