Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to keep google map api centered on marker responsive

I have used the google map api for a map which I have placed a marker on. I have tried to keep the marker focused on using the addDomListener however this does not work and the marker is in range of the map div, but not in the center of the map as desired when re-sizing the screen.

As my map is not the only div on the row, does this mean my css is making this happen?

Here is my js

function initMap() {
    var myLatLang = {lat: 51.46674, lng: -0.18976};

    var mapDiv = document.getElementById('map');
    var map = new google.maps.Map(mapDiv, {
        center: myLatLang,
        zoom: 17
    });

    var marker = new google.maps.Marker({
        position: myLatLang,
        map:map,
        title: 'Decorum Bulidng Consultancy'
    });

    var center;
    function calculateCenter() {
    center = map.getCenter();
    }
    google.maps.event.addDomListener(window, 'idle', function() {
        calculateCenter();
    });
    google.maps.event.addDomListener(window, 'resize', function() {
    map.setCenter(center);
    });

}

here is my css

#map{
    position: relative;
    padding-bottom: 75%; // This is the aspect ratio
    height: 0;
    overflow: hidden;
}

and here is the html

            <div class="news-image">
                <div id="map"></div>
            </div>
            <div class="verticalLine"></div>
            <div class="contact">
                <h2>Contact Info</h2>

                <ul class="contact-info">
                    <li class="phone"><a href="tel:0734">0734</a></li>
                    <li class="email"><a href="mailto:toby@d.com">toby@d.com</a></li>
                    <li class="address">
                        <a href="/contact-us"> 
                            <p>360</p>
                            <p>London</p>
                            <p>SW6</p>
                            <p>England</p>
                        </a>
                    </li>

                </ul>
            </div>
            <div class="horizontalLine"></div>