Google Maps hide controls on small screens

荒凉一梦 提交于 2019-12-25 08:14:36

问题


I want to hide Google Maps controls on small screens. By default Google Maps hides them at 320px width i think, but i need them to hide on bigger width, around 400px. Here is one working example, but i am using different code so it is not working, i just get syntax errors. Here is my code:

var map = new google.maps.Map(document.getElementById('map-canvas'), {
     center: {
       lat: 52.00,
       lng: 10.00
     },
     zoom: 4,
      zoomControlOptions: {
              position: google.maps.ControlPosition.LEFT_TOP
          },
        mapTypeControl: true,
        mapTypeControlOptions: {
              position: google.maps.ControlPosition.TOP_RIGHT
          }
   });

This is code which captures width and disables controls, and it works with most Google Maps codes, but not with my...

$(window).width()       
var width = window.innerWidth;
 if(width < 400){
            mapOptions.mapTypeControl = false;
            mapTypeControl: false
            disableDefaultUI: true
            delete mapOptions.mapTypeControlOptions;
        }

Please show me how to combine these two codes so they work, i tryed doing something on my own, but im just getting syntax errors.


回答1:


Simply hide it with media queries when the screen gets too small ;).

@media screen and (max-width: 400px) {
    .gm-style-mtc {
        display:none;
    }   

}



回答2:


You can use Javascript window.matchMedia() method to match your media-query:

if(window.matchMedia("(your-media-query)").matches){
  mapOptions.mapTypeControl = false
  disableDefaultUI: true
  delete mapOptions.mapTypeControlOptions;
}

More infos here: https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia



来源:https://stackoverflow.com/questions/39638274/google-maps-hide-controls-on-small-screens

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