doublesecretagency / craft-googlemaps

Google Maps plugin for Craft CMS - Maps in minutes. Powered by the Google Maps API.
https://plugins.doublesecretagency.com/google-maps/
Other
10 stars 8 forks source link

Issue with openInfoWindow #57

Closed idawgik closed 2 years ago

idawgik commented 2 years ago

Hey, so I have a map with a single marker that I want to open the info window by default on page load.

I see the documented function here: https://plugins.doublesecretagency.com/google-maps/dynamic-maps/universal-methods/#openinfowindow-markerid

However, I am unable to get it to actually work in my code and not sure what I'm doing wrong.

Here's my twig code:

        {% set address = contactInformation.location.location %}
        {{ googleMaps.map(address, {
            'id': 'footer-map',
            'height': 500,
            'styles': styleSet,
            'infoWindowTemplate': '_layout/_info_window'
        }).zoom(17).tag() }}
        {% set map = googleMaps.getMap('footer-map') %}
        {% do map.openInfoWindow('*') %}

It doesn't seem to actually run the map.openInfoWindow function. I've also tried doing this with javascript, however it seems the scripts the Google Maps plugin inserts on the page are added AFTER any custom JS I add, so I'm unable to access the map since it's not loaded yet.

I'm probably missing something obvious, but any assistance is appreciated.

I'm on Craft 4.0.5.2 with Google Maps 4.2.3

lindseydiloreto commented 2 years ago

Not sure if this is exactly the problem, but try chaining the openInfoWindow parameter right after the zoom parameter...

{{ googleMaps.map(address, {
        'id': 'footer-map',
        'height': 500,
        'styles': styleSet,
        'infoWindowTemplate': '_layout/_info_window'
    })
    .zoom(17)
    .openInfoWindow('*')
    .tag()
}}

If that doesn't do the trick, ping me on Craft Discord, it'll be easier to troubleshoot it together there. If the solution is notable, we can circle back and post the answer in this thread.

idawgik commented 2 years ago

Got this resolved over Discord. Had to put the code to open the info window in a callback on the .tag() method.

Working code:

        {% set address = contactInformation.location.location %}
        {{ googleMaps.map(address, {
            'id': 'footer-map',
            'height': 500,
            'styles': styleSet,
            'infoWindowTemplate': '_layout/_info_window'
        }).zoom(17).tag({
            'callback': 'openInfoWindow'
        }) }}

{% js %}

function openInfoWindow(){
    var map = googleMaps.getMap('footer-map');
    map.openInfoWindow('*');
}

{% endjs %}

Thanks for the help!

lindseydiloreto commented 2 years ago

Awesome, no problem! Feel free to ping me on Discord in the future, it's the easiest way to reach me. 🙂