【问题标题】:Google Maps v3 hide elements (roads, roadsigns, etc)Google Maps v3 隐藏元素(道路、路标等)
【发布时间】:2010-10-26 14:24:33
【问题描述】:

我在http://www.41latitude.com/post/1268734799/google-styled-maps找到了一个代码sn-p:

[
  {
    featureType: "administrative",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
  },{
    featureType: "poi",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
  },{
    featureType: "water",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
  },{
    featureType: "road",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
  }
]

我应该可以在我的地图中使用它,但是有人可以告诉我如何使用这个 sn-p 吗?我在 Google Maps V3 的 API 中找不到任何相关信息。

【问题讨论】:

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


【解决方案1】:

我知道这已经 5 岁了,但我偶然发现了这一点,并且在我看来,公认的解决方案比需要的要复杂得多。鉴于原始帖子中的 JSON,这就是您将样式应用于现有地图的方式:

var mapStyle = [
  {
    featureType: "administrative",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
  },{
    featureType: "poi",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
  },{
    featureType: "water",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
  },{
    featureType: "road",
    elementType: "labels",
    stylers: [
      { visibility: "off" }
    ]
  }
]

//create map
var map = new google.maps.Map(...); //This assumes you already have a working map

//set style
map.set('styles', mapStyle);

【讨论】:

    【解决方案2】:

    作为@ceejayoz suggested in the other answer,您正在尝试使用v3 Maps API 的新Styled Map features。这是一个非常基本的示例,说明如何在简单的地图中使用上述样式:

    <!DOCTYPE html>
    <html> 
    <head> 
       <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
       <title>Google Maps Dark Water Style Demo</title> 
       <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> 
    </head> 
    <body> 
       <div id="map" style="width: 550px; height: 300px;"></div> 
    
       <script type="text/javascript"> 
         var myStyle = [
           {
             featureType: "administrative",
             elementType: "labels",
             stylers: [
               { visibility: "off" }
             ]
           },{
             featureType: "poi",
             elementType: "labels",
             stylers: [
               { visibility: "off" }
             ]
           },{
             featureType: "water",
             elementType: "labels",
             stylers: [
               { visibility: "off" }
             ]
           },{
             featureType: "road",
             elementType: "labels",
             stylers: [
               { visibility: "off" }
             ]
           }
         ];
    
         var map = new google.maps.Map(document.getElementById('map'), {
           mapTypeControlOptions: {
             mapTypeIds: ['mystyle', google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN]
           },
           center: new google.maps.LatLng(30, 0),
           zoom: 3,
           mapTypeId: 'mystyle'
         });
    
         map.mapTypes.set('mystyle', new google.maps.StyledMapType(myStyle, { name: 'My Style' }));
       </script> 
    </body> 
    </html>
    

    截图:

    您可能还想查看Google Maps APIs Styling Wizard,它允许您以图形方式编辑样式。

    【讨论】:

    • 太好了,这个例子是一个很好的例子,给了我解决方案。
    • 是否可以这样做:featureType: ["poi", "transit"] 即全部选择它们,然后同时禁用它们的可见性?
    • 可以,使用featureType: "all"
    【解决方案3】:

    您链接到的页面包含指向the Google Maps API documentation for this feature 的链接。

    【讨论】:

    • 好的,错过了链接。就像我女朋友说的:我是一个很好的搜索者,但一个糟糕的发现者!
    猜你喜欢
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    相关资源
    最近更新 更多