将谷歌地图InfoWindow设置为超过700px宽度

时间:2014-12-24 08:03:49

标签: javascript jquery css css3 infowindow

我想要显示的InfoWindow的内容是840px x 660px。通过在构造函数中设置MaxWidth属性

(ex. new google.maps.InfoWindow({ content: some_text, maxWidth: 840});)

设置宽度为707px的窗口。 我已设法覆盖.gm-style-iw#content的类(我的内容在div中,正确设置了宽度和高度),但#content div标签之间有一些div标记和div标签,类.gm-style-iw,宽度最大为707px,高度为最大645px(这些值是我在Chrome中的“Inspecting Element”中找到的)。

如果我删除了主要div标记的整个样式(在Chrome Inspect Element中)(其中包含至少7-8个div子标记,其中div标记为类.gm-style-iw#content div tag)和所有没有指定类名也没有指定id的子标签,那么窗口看起来很好,但是我无法设法在类或javascript中设置宽度和高度... 有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:2)

不要传递840px传递值,例如840。如果您使用840px,则会收到如下错误:

identifier starts immediately after numeric literal

你应该用这个:

var infowindow = new google.maps.InfoWindow({
      content: contentString,
      maxWidth: 840
  });

看一个小型演示:

function initialize() {
  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var mapOptions = {
    zoom: 4,
    center: myLatlng
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var contentString = '<div id="content">'+
      '<div id="siteNotice">'+
      '</div>'+
      '<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
      '<div id="bodyContent">'+
      '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
      'sandstone rock formation in the southern part of the '+
      'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+
      'south west of the nearest large town, Alice Springs; 450&#160;km '+
      '(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+
      'features of the Uluru - Kata Tjuta National Park. Uluru is '+
      'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
      'Aboriginal people of the area. It has many springs, waterholes, '+
      'rock caves and ancient paintings. Uluru is listed as a World '+
      'Heritage Site.</p>'+
      '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
      'http://en.wikipedia.org/w/index.php?title=Uluru</a> '+
      '(last visited June 22, 2009).</p>'+
      '</div>'+
      '</div>';

  var infowindow = new google.maps.InfoWindow({
      content: contentString,
      maxWidth: 840
  });

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'Uluru (Ayers Rock)'
  });
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
   html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=drawing,geometry"></script>
<div id="map-canvas"></div>