Google Maps API - 仅显示流量层?

时间:2014-11-20 20:13:38

标签: google-maps

我正在使用Google Maps API显示我所在区域的交通状况,我想知道是否可以隐藏它的实际地图部分,以便只有交通图层可见(绿色/红色/黄色交通车道)和道路名称将是可见的,但背景将是透明的或纯色而不是显示地图。)

这是我到目前为止所拥有的:

$(function() {
    var map = new google.maps.Map(document.getElementById("map"),
    {
        zoom: 12,
        center: new google.maps.LatLng(34.0204989,-118.4117325),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDoubleClickZoom: true,
        draggable: false,
        scrollwheel: false,
        panControl: false,
        disableDefaultUI: true
    });

    var trafficLayer = new google.maps.TrafficLayer();
    trafficLayer.setMap(map);
});

这是我想要完成的一个粗略的例子: enter image description here

JSFiddle

2 个答案:

答案 0 :(得分:7)

styled map wizard上,这给了我一张空白地图。

[
  {
    "stylers": [
      { "visibility": "off" }
    ]
  }
]

proof of concept fiddle

代码段

$(function() {
  var map = new google.maps.Map(document.getElementById("map"), {
    zoom: 12,
    center: new google.maps.LatLng(34.0204989, -118.4117325),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDoubleClickZoom: true,
    draggable: false,
    scrollwheel: false,
    panControl: false,
    disableDefaultUI: true,
    styles: [{
      "stylers": [{
        "visibility": "off"
      }]
    }]
  });

  var trafficLayer = new google.maps.TrafficLayer();
  trafficLayer.setMap(map);
});
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map" style="border: 2px solid #3872ac;"></div>

答案 1 :(得分:1)

你可以试试这个:

    var map = new google.maps.Map(document.getElementById("map-canvas"),
{
    zoom: 12,
    center: new google.maps.LatLng(34.0204989,-118.4117325),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    styles: [
        {
            featureType: 'poi',
            stylers: [
                { visibility: 'off' }
            ]
        },
        {
            featureType: 'road',
            stylers: [
                { visibility: 'off' }
            ]
        },
        {
            featureType: 'transit',
            stylers: [
                { visibility: 'off' }
            ]
        },
        {
            featureType: 'landscape',
            stylers: [
                { visibility: 'off' }
            ]
        },
        {
            elementType: 'labels',
            stylers: [
                { visibility: 'on' }
            ]
        }
    ]

});

var trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);