当前位置: 首页 > 知识库问答 >
问题:

在谷歌地图信息窗口-vuejs中包含路由器链接

费锋
2023-03-14

我正在用VueJS在Laravel 5.7中构建一个应用程序。我有一个谷歌地图(使用vue2谷歌地图构建),有200个标记,每个标记都有一个信息窗口。我想在infoWindow中包含一个路由器链接,但由于该链接是作为字符串传递的,所以Vue似乎没有对其进行解析。

这是我目前的组件-除了路由器链接,所有的工作

有人能建议一种方法来解析路由器链接吗?

谢谢你

<template>
<div id="map">

    <gmap-map
            :center="center"
            :zoom="11"
            style="width:100%;  height: 750px;"
            map-type-id="satellite"
    >

        <GmapMarker v-for="location in locations"
                    :key="location.id"
                    @click="toggleInfoWindow(location, location.id)"
                    :position="({
                        lat : location.latitude,
                        lng : location.longitude,
                        }
                    )"
        ></GmapMarker>
        <gmap-info-window
                :options="infoOptions"
                :position="infoWindowPos"
                :opened="infoWinOpen"
                @closeclick="infoWinOpen=false"
        >
            <div v-html="infoContent"></div>
        </gmap-info-window>

    </gmap-map>


</div>
</template>

<script>

    export default {

        data() {
            return {
                center: {lat: 51.553726, lng: -0.110035},
                locations: [],
                visits:[],
                infoContent: '',
                infoWindowPos: {
                    lat: 0,
                    lng: 0
                },
                infoWinOpen: false,
                currentMidx: null,
                //optional: offset infowindow so it visually sits nicely on top of our marker
                infoOptions: {
                    pixelOffset: {
                        width: 0,
                        height: -35
                    }
                }
            }

        },

        methods: {
            getLocations(){

                window.axios.get('/api/locations').then(({data})=>{
                    data.forEach(location => {
                        this.locations.push(location)
                    });
                });
            },


            toggleInfoWindow: function (marker, idx) {

                this.infoWindowPos = ({
                        lat : marker.latitude,
                        lng : marker.longitude,
                    }
                );
                this.infoContent = this.getInfoWindowContent(marker);

                //check if its the same marker that was selected if yes toggle
                if (this.currentMidx == idx) {
                    this.infoWinOpen = !this.infoWinOpen;
                }
                //if different marker set infowindow to open and reset current marker index
                else {
                    this.infoWinOpen = true;
                    this.currentMidx = idx;
                }
            },

            getInfoWindowContent: function (marker) {
                return(`<div class="info_window container">
                          <h3>${marker.name}</h3>
                          <a href="/location/${marker.slug}"><div class="mx-auto btn btn-success">More Info</div></a>
                          <router-link :to="/location/${marker.slug}" class="mx-auto btn btn-success">RL More Info</router-link>
                         </div>`);
            },


        },

        created(){
            this.getLocations()

        },


    };
</script>

<style>

</style>

共有2个答案

颜志业
2023-03-14

以下代码摘录中使用的v-html指令不能用于编写模板部分[1]

<div v-html="infoContent"></div>

在这个用例中,infoContent可以作为一个单独的组件来实现,可以通过以下方式注册[2]

<template>
<!--...-->
  <gmap-info-window
    :options="infoOptions"
    :position="infoWindowPos"
    :opened="infoWinOpen"
    @closeclick="infoWinOpen=false"
  >
    <info-content 
      v-bind:slug="marker.slug"
      v-bind:name="marker.name"
    >
    </info-content>
  </gmap-info-window>
<!--...-->
</template>

<script>
import InfoContent from './components/InfoContent.vue'

export default {
   components: {
     InfoContent
   },
   data() { /*...*/ },
   methods: { 
     //...
     toggleInfoWindow (marker, idx) {
       //...
       this.marker = marker;
       //...
     }
   }
}
</script>

组件/InfoContent.vue

<template>
   <div class="info_window container">
    <h3>{{ name }}</h3>
    <a href="/location/{{ slug }}">
      <div class="mx-auto btn btn-success">More Info</div>
    </a>
    <router-link
      :to="/location/{{ slug }}"
      class="mx-auto btn btn-success">
      RL More Info
    </router-link>
  </div>
</template>

<script>
export default {
  name: 'info-content',
  props: {
    slug: String,
    name: String
  }
}
</script>
钱弘壮
2023-03-14

v-html指令只是将传递给它的数据作为该元素的innerHtml直接传递到DOM中。如果您想传递已经是html的内容,这非常好,但如果Vue需要对其进行处理,这就不太好了。

相反,您必须使用模板本身。如果它在组件本身,就像这里,这很简单。与其生成一些html字符串,不如将整个内容放在模板中,并使用小胡子符号、v-if等来控制在何处显示的内容。如果您有一个单独的组件,您可以使用插槽来控制传递到信息窗口的内容。然后通过父级模板将内容传递到该插槽。

要解决您的问题,您必须这样做。首先将您的标记指定给我们可以在模板中使用的某个变量。在这种情况下,我们可以重复使用infoContent

    toggleInfoWindow: function (marker, idx) {
        this.infoWindowPos = ({
                lat : marker.latitude,
                lng : marker.longitude,
            }
        );
        this.infoContent = marker;

        //check if its the same marker that was selected if yes toggle
        if (this.currentMidx == idx) {
            this.infoWinOpen = !this.infoWinOpen;
        }
        //if different marker set infowindow to open and reset current marker index
        else {
            this.infoWinOpen = true;
            this.currentMidx = idx;
        }
    },

现在,我们修改模板,使您的html和组件位于gmap信息窗口中。由于我们将活动标记指定给了infoContent,所以我们在这里引用它。

    <gmap-info-window
            :options="infoOptions"
            :position="infoWindowPos"
            :opened="infoWinOpen"
            @closeclick="infoWinOpen=false"
    >
        <div class="info_window container">
            <h3>{{ infoContent.name }}</h3>
            <a :href="`/location/${infoContent.slug}`"><div class="mx-auto btn btn-success">More Info</div></a>
            <router-link :to="`/location/${infoContent.slug}`" class="mx-auto btn btn-success">RL More Info</router-link>
        </div>
    </gmap-info-window>

现在,当您调用toggleInfoWindow时,信息窗口现在将使用新的标记信息更新。

 类似资料:
  • 我想创建一个谷歌地图标记列表,这些标记填充关于这些特定位置的信息窗口,并在信息窗口内链接到维基百科关于该位置的文章。我在这个单击函数中进行Ajax调用,并且这个函数在一个

  • 我正在尝试自定义信息窗口,并在加载地图时显示它。自定义信息窗口出现了,但是,除了默认的信息窗口背景(白色)之外,我还没有为出现的标记设置图标?为什么我会得到这个默认值? 以下是我尝试的内容的屏幕截图: 我不想要白色窗口和标记图标?我正在尝试实现以下截图:如何使用Android map API v2创建自定义形状的位图标记 以下是我正在尝试的:

  • 我正在使用osmdroid和osmbonuspack库。在活动中,我有一个地图,上面有几个来自osmbonuspack的标记,每个标记在点击时打开一个InfoWindow。但当标记接近MapView的上边缘时,它的infowindow打开超出界限。有没有一种方法可以调整mapview,以便让Marker的InfoWindow像这个iOS库一样完整地呈现出来?https://www.mapbox.c

  • 我想为谷歌地图上的每个标记创建一个自己的窗口。我尝试了这个: 不幸的是,只有第二个标记有一个包含的窗口。如果我单击第一个标记,那么带有的第二个标记的窗口就会弹出。我不明白为什么会发生这种情况。 我注意到,如果我为第二个标记和第二个窗口使用一个新变量,每个标记都有自己的窗口,如下所示: 但我完全不明白为什么我需要使用新的变量。为什么我不能覆盖旧的变量? 注意:这个问题是关于如何获取多个不同的info

  • 升级到NativeScript6后,在iOS上点击谷歌地图上的标记以显示相关信息窗口,应用程序就会崩溃。这是由于_layoutrootview被删除-请参见https://github.com/dapriett/nativescript-google-maps-sdk/issues/354 问题是没有提供任何替代方案,google-maps-sdk插件最近也没有任何活动。 按照@manoj的指示,

  • 我想从谷歌地图API接收道路信息,就像普通的街道信息一样。 例如,在谷歌地图应用程序中,您可以看到每条道路的交通情况。是否有任何方法可以在数据中接收此信息,而不仅仅是在屏幕上查看? 我需要得到的信息,如名称,交通,限速和起点和终点。 我考虑一些类似于现有街道信息的东西,我会给出一个位置(long,lat),系统会返回附近最近的道路(也可能是十字路口)。 有没有办法通过谷歌地图API获取这些信息?如