0

I'm using Google Maps JavaScript API v3 and I want to show three maps on three tabs in the same page. If I show all the maps on the tab default everything is right, but when I try to load each map in each tab only works fine the map showed in the tab default, the others two maps doesn't show.

I have looking for any bug or information about this issue and I have find that If I resize the map when I try to access to the tab this would be works fine, but to me, doesn't work fine :(

So, I don't know what happened. I'm not using Bootstrap.

Javascript code

 function initMap() { var mapOptions = { center: new google.maps.LatLng(40.0504, -3.3240), zoom: 6, mapTypeId: 'terrain' }; var mapOptions2 = { center: new google.maps.LatLng(39.3204, 2.8240), zoom: 7, mapTypeId: 'terrain' }; var mapOptions3 = { center: new google.maps.LatLng(28.4504, -15.7240), zoom: 7, mapTypeId: 'terrain' } map = new google.maps.Map(document.getElementById('map'), mapOptions); map2 = new google.maps.Map(document.getElementById('map2'), mapOptions2); map3 = new google.maps.Map(document.getElementById('map3'), mapOptions3); google.maps.event.addListener(map, 'click', function(event){ document.getElementById("location").innerHTML=event.latLng; }); }; /** * Resize function to try to show the map on tab **/ function resizeMap2(){ google.maps.event.trigger(map2, 'resize'); map2.setZoom(map2.getZoom()); }; 

Am I doing something wrong?

1 Answer 1

1

It's kind of hard to say what the problem is without seeing your full code. You appear to be creating the google map objects correctly.

See https://www.w3schools.com/howto/howto_js_tabs.asp for a better explanation of the tabs code used in the example below.

Essentially the only thing I added was a resize trigger at the bottom of the openTab function so that it gets called after the tab has changed (like you were doing with resizeMap2 in your code).

Runnable example (You'll need to add your own google maps api key): https://jsfiddle.net/htmp3Lgw/12/

Hope it helps!


JS Fiddle HTML

<body> <h3>My Google Maps Demo</h3> <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">Map 1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">Map 2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">Map 3</button> </div> <div id="tab1" class="tabcontent"> <div id="map"></div> </div> <div id="tab2" class="tabcontent"> <div id="map2"></div> </div> <div id="tab3" class="tabcontent"> <div id="map3"></div> </div> <script> var map; var map2; var map3; // ======= Google maps code ======= function initMap() { var uluru = {lat: -25.363, lng: 131.044}; map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: uluru }); map2 = new google.maps.Map(document.getElementById('map2'), { zoom: 4, center: uluru }); map3 = new google.maps.Map(document.getElementById('map3'), { zoom: 4, center: uluru }); } // ======= Resize Maps Trigger ======= function doMapsResize() { var i; var maps = [map, map2, map3]; for (i = 0; i < maps.length; i++) { google.maps.event.trigger(maps[i], 'resize'); } } // ======= Tabs code ======= function openTab(evt, tabId) { // Declare all variables var i, j, tabcontent, tablinks; // Get all elements with class="tabcontent" and hide them tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } // Get all elements with class="tablinks" and remove the class "active" tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } // Show the current tab, and add an "active" class to the button that opened the tab document.getElementById(tabId).style.display = "block"; evt.currentTarget.className += " active"; // Finally, after we've changed tabs visibility call the resize trigger doMapsResize(); } </script> <!-- ADD YOUR OWN GOOGLE MAPS KEY --> <script async defer src="https://maps.googleapis.com/maps/api/js?key=< YOUR GOOGLE MAPS API KEY >&callback=initMap"> </script> 

JS Fiddle CSS

 /* ========== Google Maps Styles =========== */ #map { height: 400px; width: 100%; } #map2 { height: 400px; width: 100%; } #map3 { height: 400px; width: 100%; } /* ========== Tabs Styles =========== */ /* Style the tab */ div.tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } /* Style the buttons inside the tab */ div.tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } /* Change background color of buttons on hover */ div.tab button:hover { background-color: #ddd; } /* Create an active/current tablink class */ div.tab button.active { background-color: #ccc; } /* Style the tab content */ .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Jack!!! Thanks for your solution!!! It works!!! The main difference between my code and your is that I don't use javascript to show o hide tabs, I do it through CSS and when I access to one tab I only resize the map of this tab and not all the maps.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.