0

I have seen that this is a common problem, but I can't seem to find a solution on my specific project. The first map opens, but when I click the second tab the map opens but it looks like this screenshot enter image description here

Here is the link http://cotdigtest5.com/contact.php . You can see the problem when you click on the tabs.

The javascript is:

function displayMap1() {
    document.getElementById('map-canvas1').style.display="block";
    initialize1();
}
 function initialize1() {
    var myLatlng = new google.maps.LatLng(6.441648,3.419913);
    var myOptions = {   
        zoom: 14,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map-canvas1"),
                                    myOptions);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map
    })
 }

function displayMap2() {
    document.getElementById('map-canvas2').style.display="block";
    initialize2();
}
function initialize2() {
    var myLatlng = new google.maps.LatLng(6.441648,3.419913);
    var myOptions = {   
        zoom: 14,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map-canvas2"),
                                    myOptions);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map
    })
}

<li><a href="#content-1" onclick="displayMap1()">Map 1</a></li>
<li><a href="#content-2" onclick="displayMap2()">Map 2</a></li>
Klevis Miho
  • 851
  • 8
  • 15

1 Answers1

0

I managed to find a solution. It doesn't feel right, but works. Basically a the initialize function is run on a fast interval.

var refreshIntervalId;

function displayMap1() {
    document.getElementById('map-canvas1').style.display="block";
    refreshIntervalId = setInterval(function () { initialize1() }, 100);
}
function initialize1() {
    clearInterval(refreshIntervalId);
    var myLatlng = new google.maps.LatLng(6.441648,3.419913);
    var myOptions = {   
        zoom: 14,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map-canvas1"),
                                    myOptions);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map
    })
}


function displayMap2() {
    document.getElementById('map-canvas2').style.display="block";
    refreshIntervalId = setInterval(function () { initialize2() }, 100);
}
function initialize2() {
    clearInterval(refreshIntervalId);
    var myLatlng = new google.maps.LatLng(6.441648,2.419913);
    var myOptions = {   
        zoom: 14,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map-canvas2"),
                                    myOptions);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map
    })
}
Klevis Miho
  • 851
  • 8
  • 15