1

似乎页面加载上谷歌地图信息框的位置是错误的,或者当您将地图高度设置为低于 400/380 像素时至少是错误的示例 http://jsfiddle.net/TbDzG/18/

 var startLocation = new google.maps.LatLng(36.151685,-115.152438);

function initialize() {
    var mapOptions = {
        center: startLocation,
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map-canvas"),
    mapOptions);

  var marker = new google.maps.Marker({
      position: startLocation,
      map: map,
      draggable:true,
      animation: google.maps.Animation.DROP,
      title:"Hello World!"
  });
  var infowindow = new google.maps.InfoWindow();
infowindow.setContent('some content here');
infowindow.open(map, marker);  


}
google.maps.event.addDomListener(window, 'load', initialize);

有谁知道如何将其移动到正确的位置并使信息框居中而不是使标记居中的技巧?

4

2 回答 2

5

您不能将信息窗口居中,因为标记和信息窗口 (+map) 具有相同的位置。

试试这个,当加载地图时,信息窗口将打开并平移地图。

var infowindow = new google.maps.InfoWindow();
infowindow.setContent('some content here');
google.maps.event.addListenerOnce(map, 'idle', function(){infowindow.open(map, marker);  });

http://jsfiddle.net/iambnz/jAgcz/

于 2013-07-14T21:18:03.450 回答
1

如果您在单击时打开信息窗口并在地图完成渲染(“空闲”事件)后触发对标记的单击,则地图将移动,因此信息窗口将完全显示。

var startLocation = new google.maps.LatLng(36.151685,-115.152438);

function initialize() {
    var mapOptions = {
        center: startLocation,
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map-canvas"),
    mapOptions);

  var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
      position: startLocation,
      map: map,
      draggable:true,
      animation: google.maps.Animation.DROP,
      title:"Hello World!"
  });
  google.maps.event.addListener(marker,'click', function(evt) {
      infowindow.setContent('some content here');
      infowindow.open(map, marker);
  });
  google.maps.event.addListenerOnce(map,'idle',function() {
      google.maps.event.trigger(marker,'click');
  });  
}
google.maps.event.addDomListener(window, 'load', initialize);

http://jsfiddle.net/YWbXE/2/

于 2013-07-14T21:19:28.503 回答