Google Maps v3 hide elements (roads, roadsigns, etc)

匿名 (未验证) 提交于 2019-12-03 02:45:02

问题:

I found a code snippet on http://www.41latitude.com/post/1268734799/google-styled-maps:

[   {     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" }     ]   } ] 

I should be able to use it in my maps, but is there somebody who can tell me how I can use this snippet? I can't find anything about it in the API of Google Maps V3.

回答1:

As @ceejayoz suggested in the other answer, you are trying to use the new Styled Map features of the v3 Maps API. Here's a very basic example of how you could use the above style in a simple map:

<!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> 

Screenshot:

You may also want to check out the Google Maps APIs Styling Wizard which will allow you to graphically edit styles.



回答2:

I know this is 5 years old, but I stumbled across this and the accepted solution is much more complex than needed in my opinion. Given the JSON in the original post, this is how you would apply the style to an existing map:

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


回答3:

The page you linked to includes a link to the Google Maps API documentation for this feature.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!