Google Maps API color stylers not working

2019-07-18 04:21发布

问题:

I'm trying to learn how to use the API, and I'm following the code that's on their site completely but for some reason it just doesn't want to change colour.

The code I'm posting is just copy and pasted from their website but I cannot get it to work, can any one please tell me what I'm doing wrong?

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>maps</title>

    <style>
        html, body {
            height: 100%;
            margin: 0;
            width: 100%;

            }
        #map{
            height: 100%;
            margin: 0;
            width: 100%;
            background-color: red;
            }
    </style>

</head>
<body>

<div id="map"></div>

<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

<script>
var map;
function initialize() {
  var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map'),
      mapOptions);
}

google.maps.event.addDomListener(window, 'load', initialize);


var styleArray = [
  {
    featureType: "all",
    stylers: [
      { saturation: -80 }
    ]
  },{
    featureType: "road.arterial",
    elementType: "geometry",
    stylers: [
      { hue: "#00ffee" },
      { saturation: 50 }
    ]
  },{
    featureType: "poi.business",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
  }
];
</script>
</body>
</html>

回答1:

You must apply the style to the map:

var mapOptions = {
        zoom: 8,
        center: new google.maps.LatLng(-34.397, 150.644),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        styles: styleArray
      };