Is it possible to change Google Map styles after the map has been initialised?

爷,独闯天下 提交于 2021-01-27 03:02:00

问题


I understand how to initialise a map with custom styles like the following:

var styles =   [
    {
      featureType: "water",
      stylers: [
        { visibility: "on" },
        { color: "#ffffff" }
      ]
    }
  ];

var mapOptions = {
      zoom: 13,
      maxZoom: 15,
      minZoom: 12,
      center: new google.maps.LatLng(50.924229,-1.396841),
      disableDefaultUI: true,
      mapTypeId: google.maps.MapTypeId.ROADMAP
      styles: styles
    };
    map = new google.maps.Map(document.getElementById('map'), mapOptions);

But is it possible to change to another style once the map has already been initialised? For example changing the colours of the map when certain events are triggered?


回答1:


Yes. just use

map.setOptions(mapOptions);



回答2:


Yes, create a new style object and then change the style by setting the option: map.setOptions({styles: styles});



来源:https://stackoverflow.com/questions/11865648/is-it-possible-to-change-google-map-styles-after-the-map-has-been-initialised

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