Access a featureType within a styled mapType of Google Maps

138 Views Asked by At

In e.g. maps.amsterdam.nl/hoegroot I use different styled mapTypes: Google Light, Google Dark and Google Grey. I want to toggle all labels on/off with a button without changing the whole mapType. I'm looking for a way to only set the visibility on/off of the featureType label through Javascript:

{featureType:'label', stylers:[{visibility:'offon'}]}

How can I access a featureType within a MapType?

1

There are 1 best solutions below

0
Haan On

Get the styles of de mapTypes.

Push a style at the end of the array when labels off.

Pop this style at the end of array when labels on.

function doLabels() {
var theCurrentMapType = theMap.getMapTypeId();
var theMapTypes = ['GOOGLE_LIGHT', 'GOOGLE_DARK', 'GOOGLE_GREY'];
for (i = 0; i < theMapTypes.length; i++) { // change all maptypes, to hold labels on/off
    var theMapStyles = theMap.mapTypes[theMapTypes[i]].styles;
    if ($('#labelsonoff input').is(":checked")) theMapStyles.pop();
    else theMapStyles.push({featureType:'all', elementType:'labels', stylers:[{visibility:'off'}]});            
}   
theMap.setMapTypeId('GOOGLE_WHITE'); // change first to other maptype
theMap.setMapTypeId(theCurrentMapType);     // then back to current maptype

}