简体   繁体   English

如何使用其API样式化Google Maps

[英]How to style Google Maps with their API

I'm trying to change the style of a Google Map, but I'm not sure how. 我正在尝试更改Google Map的样式,但不确定如何。 Google has given me these object attributes: Google为我提供了以下对象属性:

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

But I am unsure how to make that work with the following code, which is a generated Google map: 但是我不确定如何使用以下代码(生成的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>

I've tried adding them to the myOptions, but it's not working: 我试过将它们添加到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 . 这是样式API的链接 But I can't make heads or tails of it. 但是我不能做它的正面或反面。 Could anyone help? 有人可以帮忙吗?

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
};

Here is your code from above in a fiddle using the stylers -> http://jsfiddle.net/cKQxb/ producing this : 这是您上面使用样式器-> http://jsfiddle.net/cKQxb/产生的代码: 在此处输入图片说明

You should put it on Option Object that you contruct your map. 您应该将其放在构建地图的“选项对象”上。 Or set this option with map.set("styles", stylesOpts). 或使用map.set(“ styles”,stylesOpts)设置此选项。

Change your code to this: 将代码更改为此:

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 https://developers.google.com/maps/documentation/javascript/reference#MapOptions

From the first example in the 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>

Or when you create the 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 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>

The accepted answer explains how to implement styles. 接受的答案说明了如何实现样式。 To generate the styles, there's a very useful styling tool which allows you to visually tweak map settings, which then spits out the JSON you need: 为了生成样式,有一个非常有用的样式工具,可让您直观地调整地图设置,然后吐出所需的JSON:

https://mapstyle.withgoogle.com/ https://mapstyle.withgoogle.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM