如何使用API​​设置Google地图的样式

时间:2014-03-07 14:21:22

标签: javascript google-maps google-maps-api-3

我正在尝试更改Google地图的样式,但我不确定如何。谷歌给了我这些对象属性:

[
  {
    "stylers": [
      { "hue": "#ff0022" },
      { "saturation": -16 },
      { "lightness": -5 }
    ]
  }
]

但我不确定如何使用以下代码来完成这项工作,该代码是生成的Google地图:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<div style="overflow:hidden;height:500px;width:700px;">
  <div id="gmap_canvas" style="height:500px;width:700px;"></div>
  <a class="google-map-data" href="http://www.addmap.org" id="get-map-data">google maps</a>
  <iframe src="http://www.embed-google-map.com/map-embed.php"></iframe>
  <a class="google-map-data" href="http://www.nuckelino.de" id="get-map-data">http://www.nuckelino.de</a></div>
  <script type="text/javascript"> function init_map(){
    var myOptions = {
        zoom:15,
        center:new google.maps.LatLng(51.8476894,-1.355176799999981),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };

        map = new google.maps.Map(document.getElementById("gmap_canvas"), 
          myOptions);

        marker = new google.maps.Marker({
          map: map,position: new google.maps.LatLng(51.8476894, -1.355176799999981)});
        infowindow = new google.maps.InfoWindow({
          content:"<div style='position:relative;line-height:1.34;overflow:hidden;white-space:nowrap;display:block;'><div style='margin-bottom:2px;font-weight:500;'>1 Market Street</div><span>1 Market Street <br> OX4 2JR  Woodstock</span></div>"
        });
        google.maps.event.addListener(marker, "click", function(){
          infowindow.open(map,marker);});infowindow.open(map,marker);}
        google.maps.event.addDomListener(window, 'load', init_map);
        </script>

我已经尝试将它们添加到myOptions中,但它不起作用:

    var myOptions = {
        zoom:15,
        center:new google.maps.LatLng(51.8476894,-1.355176799999981),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        stylers: [
        { hue: "#00ffe6" },
        { saturation: -20 }
      ]
  };

And here is the link to the styling API。但我无法做出正面或反面。有人可以帮忙吗?

4 个答案:

答案 0 :(得分:2)

您只需将样式添加到地图选项中(如注释中的链接所示):

var stylers = [{
  "stylers": [{ 
     "hue": "#ff0022"
   }, {
     "saturation": -16
   }, {
     "lightness": -5
   }]
}];
var myOptions = {
    ...
    styles: stylers
};

以下是使用样式的小提琴中的代码 - &gt; http://jsfiddle.net/cKQxb/ 产生这个: enter image description here

答案 1 :(得分:1)

您应该将它放在构建地图的Option Object上。或者使用map.set(“styles”,样式Opts)设置此选项。将代码更改为:

var myOptions = {
    zoom:15,
    center:new google.maps.LatLng(51.8476894,-1.355176799999981),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    styles: [ // The correct is STYLES not STYLERS
        { hue: "#00ffe6" },
        { saturation: -20 }
    ]
};

https://developers.google.com/maps/documentation/javascript/reference#MapOptions

答案 2 :(得分:1)

来自documentation的第一个例子:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<div style="overflow:hidden;height:500px;width:700px;">
  <div id="gmap_canvas" style="height:500px;width:700px;"></div>
  <a class="google-map-data" href="http://www.addmap.org" id="get-map-data">google maps</a>
  <iframe src="http://www.embed-google-map.com/map-embed.php"></iframe>
  <a class="google-map-data" href="http://www.nuckelino.de" id="get-map-data">http://www.nuckelino.de</a></div>
  <script type="text/javascript"> function init_map(){
var styles =[
  {
    "stylers": [
      { "hue": "#ff0022" },
      { "saturation": -16 },
      { "lightness": -5 }
    ]
  }
]
var myOptions = {
    zoom:15,
    center:new google.maps.LatLng(51.8476894,-1.355176799999981),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

    map = new google.maps.Map(document.getElementById("gmap_canvas"), 
      myOptions);
    map.setOptions({styles: styles});

    marker = new google.maps.Marker({
      map: map,position: new google.maps.LatLng(51.8476894, -1.355176799999981)});
    infowindow = new google.maps.InfoWindow({
      content:"<div style='position:relative;line-height:1.34;overflow:hidden;white-space:nowrap;display:block;'><div style='margin-bottom:2px;font-weight:500;'>1 Market Street</div><span>1 Market Street <br> OX4 2JR  Woodstock</span></div>"
    });
    google.maps.event.addListener(marker, "click", function(){
      infowindow.open(map,marker);});infowindow.open(map,marker);}
    google.maps.event.addDomListener(window, 'load', init_map);
    </script>

或者在创建地图时:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<div style="overflow:hidden;height:500px;width:700px;">
  <div id="gmap_canvas" style="height:500px;width:700px;"></div>
  <a class="google-map-data" href="http://www.addmap.org" id="get-map-data">google maps</a>
  <iframe src="http://www.embed-google-map.com/map-embed.php"></iframe>
  <a class="google-map-data" href="http://www.nuckelino.de" id="get-map-data">http://www.nuckelino.de</a></div>
  <script type="text/javascript"> function init_map(){
var styles =[
  {
    "stylers": [
      { "hue": "#ff0022" },
      { "saturation": -16 },
      { "lightness": -5 }
    ]
  }
]
var myOptions = {
    styles: styles,
    zoom:15,
    center:new google.maps.LatLng(51.8476894,-1.355176799999981),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

    map = new google.maps.Map(document.getElementById("gmap_canvas"), 
      myOptions);

    marker = new google.maps.Marker({
      map: map,position: new google.maps.LatLng(51.8476894, -1.355176799999981)});
    infowindow = new google.maps.InfoWindow({
      content:"<div style='position:relative;line-height:1.34;overflow:hidden;white-space:nowrap;display:block;'><div style='margin-bottom:2px;font-weight:500;'>1 Market Street</div><span>1 Market Street <br> OX4 2JR  Woodstock</span></div>"
    });
    google.maps.event.addListener(marker, "click", function(){
      infowindow.open(map,marker);});infowindow.open(map,marker);}
    google.maps.event.addDomListener(window, 'load', init_map);
    </script>

答案 3 :(得分:0)

接受的答案解释了如何实现样式。要生成样式,有一个非常有用的样式工具,它允许您可视地调整地图设置,然后吐出您需要的JSON:

https://mapstyle.withgoogle.com/