Google Maps API - 有没有办法为信息窗口设置pixelOffset?

时间:2018-04-13 00:22:29

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

对于信息窗口,我了解setPositionsetPixelOffset功能。是否有类似setPixelOffset的函数可以让你改变偏移? csupic.jpg并不作为一种功能存在。

3 个答案:

答案 0 :(得分:4)

最简单的方法是使用setter(或setOptions一次设置多个选项):

infowindow.set("pixelOffset", new google.maps.Size(10,10)); 

proof of concept fiddle

代码段

function initialize() {
  infowindow = new google.maps.InfoWindow({
    size: new google.maps.Size(150, 50),
    pixelOffset: new google.maps.Size(0, 0)
  });
  var myOptions = {
    zoom: 9,
    center: new google.maps.LatLng(43.4529192156, -116.22985867),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);

  map.addListener('click', function(e) {
    infowindow.setContent("<input type='button' value='change pixelOffset' id='btn' /><input id='xval' value='20' size='2' /><input id='yval' value='20'  size='2' />");
    infowindow.setPosition(e.latLng);
    infowindow.open(map);
    infowindow.addListener('domready', function() {
      google.maps.event.addDomListener(document.getElementById('btn'), 'click', function() {
        var size = new google.maps.Size(parseInt(document.getElementById('xval').value), parseInt(document.getElementById('yval').value));
        console.log("size=" + size.toString());
        infowindow.set("pixelOffset", size);
        infowindow.open(map);
      })
    });
  });
  google.maps.event.trigger(map, 'click', {
    latLng: new google.maps.LatLng(43.4529192156, -116.22985867)
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  padding: 0px;
  margin: 0px;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

答案 1 :(得分:0)

pixelOffset对象中有InfoWindowOptions个选项:

var infoWindow = new google.maps.InfoWindow({
  position: new google.maps.LatLng(-30, 90),
  content: "Hello World",
  pixelOffset: new google.maps.Size(0, -10)
});
infoWindow.open(map);

Documentation

答案 2 :(得分:0)

infoWindow初始化之后

 var infoWindow = new google.maps.InfoWindow({
    content: "Hello World",
    position: new google.maps.LatLng(lat,lng),
    disableAutoPan: true,
    maxWidth: 300,
    pixelOffset:new google.maps.Size(215, 265)
    });

您可以使用setOptions设置pixelOffset参数。希望这可以对某人有所帮助。

infoWindow.setOptions({
        pixelOffset:new google.maps.Size(215, 370)
    });