Milan Milan - 1 year ago 149
CSS Question

How can i set a Google Maps Marker to the Center while resizing the browser?

I wish you can help me to fix google map marker to the center while resizing the browser. I need responsive map, so...

Thanks for your time, i hope you can help me.

Here is the code:


<!-- Map container -->
<div id="map_container">

<!-- Map -->
<iframe id="map" src=""></iframe>
<!-- / Map -->

<!-- / Map container -->


#map_container {
margin: 200px auto;
padding: 0;
width: 90%;
height: 250px; }

#map_container #map {
position: relative;
margin-top: 0;
margin-left: 0;
width: 100%;
height: 100%;
border: none; }

-Regards, Milan.

Answer Source

You need to listen window resize event and then set center:

 google.maps.event.addDomListener(window, 'resize', function() {
       map.setCenter(new google.maps.LatLng(-25.363882, 131.044922));

The above solution works for Google Maps JavaScript API v3 But if you are only using iframe method then things will not be well for you in terms of responsive design as well as I know..

Alright, here is the working JsFiddle and the example that I slightly modified from : :

<!DOCTYPE html>
    <title>Resize event</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
        html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px;
    <script src=""></script>
        function initialize() {
            var mapOptions = {
                zoom: 12,
                center: new google.maps.LatLng(-25.363882, 131.044922)
            google.maps.event.addDomListener(window, 'resize', function () {
                map.setCenter(new google.maps.LatLng(-25.363882, 131.044922));
            var map = new google.maps.Map(document.getElementById('map-canvas'),

            var marker = new google.maps.Marker({
                position: map.getCenter(),
                map: map
        google.maps.event.addDomListener(window, 'load', initialize);
    <div id="map-canvas"></div>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download