The Google Map information does not show up after I expand the accordion panel

Whenever I expand the navigation panel on my webpage, the Google map doesn't display anything and just appears as grey. It's only when I open the console that the map starts to appear. What changes do I need to make in my code so that the map shows up with its marker and the programmed location without having to open the console? Interestingly, when I move the map outside the panel, it works perfectly. Any help in enhancing the existing code would be highly appreciated.

HTML Code:


    <button class="accordion">Navigation</button>
        <div class="panel">
            <div id="map" style="width:60%;height:300px"></div>

            </div>

CSS Code:
button.accordion {
            background-color: #eee;
            color: #444;
            cursor: pointer;
            padding: 18px;
            width: 100%;
            border: none;
            text-align: left;
            outline: none;
            font-size: 15px;
            transition: 0.4s;
        }

        button.accordion.active, button.accordion:hover {
            background-color: #ddd;
        }

        div.panel {
            padding: 0 18px;
            display: none;
            background-color: white;
        }

        div.panel.show {
            display: block;
        }

Script Code:

<script>
        var acc = document.getElementsByClassName("accordion");
        var i;

        for (i = 0; i < acc.length; i++) {
            acc[i].onclick = function(){
                this.classList.toggle("active");
                this.nextElementSibling.classList.toggle("show");
            }
        }
    </script>


    <script>
        function myMap() {
            var mapCanvas = document.getElementById("map");
            var myCenter = new google.maps.LatLng(51.508742,-0.120850);
            var mapOptions = {center: myCenter, zoom: 15};
            var map = new google.maps.Map(mapCanvas,mapOptions);
            var marker = new google.maps.Marker({
                position: myCenter,
                icon: "poi.png"
            });
            marker.setMap(map);
        }


        </script>
        <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB37us778WYnwNjHftUm3oL2oduV_WOt_E&callback=myMap"></script>

Answer №1

To ensure that your accordion display functions correctly, it is essential to include a map resize trigger in the show handler:

google.maps.event.trigger(map, "resize");

Your modified first script block should look like this:

<script>
    var acc = document.getElementsByClassName("accordion");
    var i;

    for (i = 0; i < acc.length; i++) {
        acc[i].onclick = function(){
            this.classList.toggle("active");
            this.nextElementSibling.classList.toggle("show");
            google.maps.event.trigger(map, "resize");  //added
        }
    }
</script>

For a demonstration of the solution, refer to the following fiddle: https://jsfiddle.net/vv0xtbrw/ (Refer to line 14);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What methods can be employed to prevent a custom CSS from overriding my personal styles?

Issue I've created a website with unique styles that I really like. However, I want to enhance its functionality by incorporating a custom dialog box from the BootBox library. The problem is that the extensive style sheet that comes with BootBox com ...

When encountering [Error in render: "TypeError: Cannot read property 'length' of undefined"] across various files, what could be causing it to appear in more than one instance?

Currently, I am engrossed in a Vue tutorial centered around creating a basic email application. However, during my test run, the inbox's content failed to display as expected (referencing the images attached below). The first image depicts my version, ...

What could be the reason why my message is not going through the textarea?

As I work on creating an online code editor, one feature I want to include is the ability for users to send all their code to themselves via email. However, I've run into an issue where the website doesn't send the code within the <textarea> ...

What is the best method to transfer text entered in one textarea to the output of another?

I recently picked up JavaScript again and encountered an issue while developing an app. I am struggling to pass input from one textarea to another, where the text should be converted to uppercase. I attempted to tweak code from w3, but I prefer to achieve ...

"Contrary to the body onload event, the window.onload event does not

So, this is my first time asking a question here. Hopefully I am providing all the necessary information. My issue is with using window.onload to display a page scripted in the head of my HTML code with type="text/view". When I tried putting the code in t ...

Share your message with BotFramework WebChat by simply clicking on the provided link

A chatbot I created using Microsoft Bot Framework has been integrated into my website through DirectLine: <div id="chatbot_body"></div> <script src="https://unpkg.com/botframework-webchat/botchat.js"></script> <script> ...

Struggling to incorporate infinite scroll feature into JSON script that is already functioning smoothly

After developing a phonegap application, I created a page called photos.html to fetch photos from my server using JSON. However, despite successfully retrieving all the photos stored in my MySQL database on the server with JSON, I struggled to integrate In ...

What is the best way to merge two foursquare requests into one?

I'm a newcomer here trying to work with the FourSquare API. I'm not entirely confident in my approach, but I am attempting to retrieve data from a search using "". However, I also want to gather more details about each venue, such as their hours ...

Using Typescript to create an asynchronous function without explicitly declaring a Promise

When you examine TypeScript's async function, you may notice the redundancy with "async" and "Promise<type>". public async test(): Promise<string> { return "Test"; } Is there a way to configure TypeScript to handle async types ...

Update header component dynamically upon successful login with Angular 11

I am currently using Angular 11 and facing an issue with displaying the username in the header component. The header loads before the login component, which results in the user data being stored in local storage only after the login component is loaded. As ...

Flask not serving Favicon and images to a React application

I am currently working with a Flask server and have it set up in the following manner: app = Flask(__name__, static_folder="dist/assets", static_url_path='/assets', template_folder="dist") ...

What is the correct way to utilize the `<svg>` element when loading external resources?

I have gone through numerous tutorials on how to add external SVGs to a webpage... such as this one: The code I am using looks like this: <svg> <use xlink:href="https://upload.wikimedia.org/wikipedia/commons/5/53/Skull_and_crossbones.svg">& ...

Issue: Query is not re-executing after navigatingDescription: The query is

On my screen, I have implemented a query as follows: export const AllFriends: React.FunctionComponent = () => { const navigation = useNavigation(); const { data, error } = useGetMyProfileQuery({ onCompleted: () => { console.log('h ...

"Utilizing a background image within an email using a table data cell (

Struggling to add a background image quickly for a cell within a table in order to showcase dynamic text on top of it I attempted : <td style="background-image:url('bla... But it's not working as expected. What would be the most effective m ...

AngularJS v 1.3.17: ng-click not triggering within modal dialog

It seems like I may have overlooked something here. Here is the code snippet for a module: (function (app) { 'use strict'; app.controller('modalCtrl', modalCtrl); modalCtrl.$inject = ['$scope', '$modalI ...

Angular - Implementing an Object Mapping Feature

Looking to dynamically generate parameter objects in Angular with a structure like this: [ password: {type: 'String', required: true, value: 'apassword'}, someRandomParam: {type: 'Integer', required: false, value:3} ] ...

Remove objects from an array if they share identical key values

I'm having trouble getting this to work. I have an array of objects that looks like this: let myCities = [ { value: 'Barcelona', code: 02342837492482347 }, { value: 'Rome', code: 28282716171819 }, { v ...

Tips for utilizing angular ui.layout in column mode: Implementing a bottom-aligned element

Recently, I began utilizing angular ui-layout to enable pane splitting within a user interface. I am attempting to design a sidebar that features a blue element at the bottom. Is there a way to manipulate the ui-layout directive to accomplish this? I exp ...

How can I convert a MongoDB document into a DTO in NestJS?

I'm currently working with a data layer that interacts with a MongoDB database. My goal is to only handle MongoDB documents in this layer without exposing the implementation details to my services. My current approach involves the following code: // ...

Spots on my Links in Navigation Menu

Today I dabbled in some basic HTML/CSS work. I was in the process of creating a simple navbar with floated links. Once I got it up and running, I encountered an issue that has me stumped, and I haven't been able to find a solution yet. The problem li ...