Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering VueJS components into a Google Map Infowindow

I'm trying to render a vue js component which is simply -

var infowindow_content = "<google-map-infowindow ";
infowindow_content += "content='Hello World'";
infowindow_content += "></google-map-infowindow>";

by passing it into the marker's infowindow

this.current_infowindow = new google.maps.InfoWindow({
    content: infowindow_content,
});
this.current_infowindow.open(context.mapObject, marker);

And the vueJS component being -

<template>
    <div>
        {{content}}
    </div>
</template>

<script>
module.exports = {
    name: 'google-map-infowindow',
    props: [ 
        'content',
    ],
}
</script>

However, this doesn't work and the window is blank.

like image 349
SagunKho Avatar asked Apr 29 '18 16:04

SagunKho


1 Answers

After revisiting this today I was able to do this by programmatically creating an instance of the vue component and mounting it before simply passing its rendered HTML template as the infowindow's content.

InfoWindow.vue

<template>
    <div>
        {{content}}
    </div>
</template>

<script>
module.exports = {
    name: 'infowindow',
    props: [ 
        'content',
    ],
}
</script>

And in the portion of the code that is required to create before opening the info-window:

...
import InfoWindowComponent from './InfoWindow.vue';
...

var InfoWindow = Vue.extend(InfoWindowComponent);
var instance = new InfoWindow({
    propsData: {
        content: "This displays as info-window content!"
    }
});

instance.$mount();

var new_infowindow = new google.maps.InfoWindow({
    content: instance.$el,
});

new_infowindow.open(<map object>, <marker>);

Note: I haven't experimented with watchers and event-driven calls for this.

like image 82
SagunKho Avatar answered Nov 18 '22 13:11

SagunKho