如何使用API​​调整Google地图的样式 [英] How to style Google Maps with their API

查看:93
本文介绍了如何使用API​​调整Google地图的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变Google Map的风格,但我不知道如何。 Google给了我这些对象属性:

I'm trying to change the style of a Google Map, but I'm not sure how. Google has given me these object attributes:

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

但我不确定如何使用以下代码工作, map:

But I am unsure how to make that work with the following code, which is a generated Google map:

<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,但它不起作用:

I've tried adding them to the myOptions, but it's not working:

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

这里是指向造型API的链接。但我无法做出正面或反面的评论。任何人都可以提供帮助吗?

And here is the link to the styling API. But I can't make heads or tails of it. Could anyone help?

推荐答案

只需将样式表添加到地图选项中即可(如注释中的链接所示) p>

You simply add the stylers to your map options (as the links in the comments explains) :

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

这里是您从上面的代码中使用stylers - > http://jsfiddle.net/cKQxb/ 产生这样的结果:

Here is your code from above in a fiddle using the stylers -> http://jsfiddle.net/cKQxb/ producing this :

这篇关于如何使用API​​调整Google地图的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆