Google Maps in Grayscale

后端 未结 8 632
误落风尘
误落风尘 2021-01-30 05:01

Is there any way to display a Google Map (embedded via the Javascript API) in grayscale without losing any other functionality?

8条回答
  •  长情又很酷
    2021-01-30 05:47

    Yes, in V3 of the api they have introduced StyledMaps.

    They've even provided a tool for you to generate the code for the styles you like. Slide the "Saturation" all the way down and you've got grayscale going on!

    The following example displays a grayscale map of Brooklyn:

    var map;
    var brooklyn = new google.maps.LatLng(40.6743890, -73.9455);
    
    var stylez = [
        {
          featureType: "all",
          elementType: "all",
          stylers: [
            { saturation: -100 } // <-- THIS
          ]
        }
    ];
    
    var mapOptions = {
        zoom: 11,
        center: brooklyn,
        mapTypeControlOptions: {
             mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'tehgrayz']
        }
    };
    
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    
    var mapType = new google.maps.StyledMapType(stylez, { name:"Grayscale" });    
    map.mapTypes.set('tehgrayz', mapType);
    map.setMapTypeId('tehgrayz');
    

提交回复
热议问题