Google maps set different zoom based on window resize

£可爱£侵袭症+ 提交于 2020-01-01 03:30:07

问题


Following to my previous question Google maps responsive resize I would like to implement a different zoom level based on windows resize, while resizing, so if browser windows is "less than" apply x number of zooming and if "major than" apply another zooming level automatically and on the fly.

The following code works perfectly in regards of recentering the map on windows resize, I need to apply the new zoom levels to it:

var map; 
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); 
  map.setZoom(zoom level needs to be automatically set according to window size);
});

回答1:


Construct the area you want to always be in view by creating a LatLngBounds and call map.fitBounds() as suggested by geocodezip.



来源:https://stackoverflow.com/questions/18446429/google-maps-set-different-zoom-based-on-window-resize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!