Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps responsive resize

I'm trying to get google maps responsive and resize while keeping its center when windows resizes. I read other stack questions in regards such as:

Responsive Google Map? and Center Google Maps (V3) on browser resize (responsive)

from the second stack question I got a link which helps me with part of the code but I still have no luck. This is the code I am using, when I resize the window, the maps doesn't resize at all

    function initialize() {       var mapOptions = {        center: new google.maps.LatLng(40.5472,12.282715),        zoom: 6,        mapTypeId: google.maps.MapTypeId.ROADMAP       };       var map = new google.maps.Map(document.getElementById("map-canvas"),                 mapOptions);     }     google.maps.event.addDomListener(window, 'load', initialize);     google.maps.event.addDomListener(window, "resize", function() {      var center = map.getCenter();      google.maps.event.trigger(map, "resize");      map.setCenter(center);      }); 

html

 <div id="map-canvas"/> 

css

html { height: 100% } body { height: 100%; margin: 0; padding: 0; } #map-canvas { height: 100%; } 
like image 680
rob.m Avatar asked Aug 26 '13 12:08

rob.m


People also ask

How do I zoom in and embed in Google Maps?

you can use zoom variable for zoom level like bellow. Visit Google maps google.com/maps to create map (Step by step: 1) Find location 2) Click the cog symbol in the top left corner and select "Share or embed map" 3) On modal window select "Embed map" 4) Copy iframe code and paste it).


Video Answer


1 Answers

Move your map variable into a scope where the event listener can use it. You are creating the map inside your initialize() function and nothing else can use it when created that way.

var map; //<-- This is now available to both event listeners and the initialize() function function initialize() {   var mapOptions = {    center: new google.maps.LatLng(40.5472,12.282715),    zoom: 6,    mapTypeId: google.maps.MapTypeId.ROADMAP   };   map = new google.maps.Map(document.getElementById("map-canvas"),             mapOptions); } google.maps.event.addDomListener(window, 'load', initialize); google.maps.event.addDomListener(window, "resize", function() {  var center = map.getCenter();  google.maps.event.trigger(map, "resize");  map.setCenter(center);  }); 
like image 95
rsnickell Avatar answered Oct 09 '22 04:10

rsnickell