【发布时间】:2012-02-12 10:34:59
【问题描述】:
我正在使用 Google Maps javascript V3 API,但在设置矩形叠加层的可见性时遇到了问题。基本上,我有一个点击监听器,在监听器里面我有这个代码:
var neLat = containmentCenter.lat()+ydiff;
var neLng = containmentCenter.lng()+xdiff;
var swLat = containmentCenter.lat()-ydiff;
var swLng = containmentCenter.lng()-xdiff;
nebound = google.maps.LatLng(neLat, neLng);
swbound = google.maps.LatLng(swLat, swLng);
crbounds = google.maps.LatLngBounds(swbound, nebound);
var containRectangleOptions = {
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: map,
bounds: crbounds,
editable: true,
visible: true
};
containRectangle.setMap(map);
containRectangle.setOptions(containRectangleOptions);
containRectangle.setVisible(true);
alert("Center: ("+containmentCenter.lat()+","+containmentCenter.lng()+")\nY:
"+ydiff+"\nX: "+xdiff+"\nVisible: "+containRectangle.getVisible());
containRectangle 是一个 Rectangle 叠加层,我在 initialize() 内部和侦听器上方定义了
containRectangle = new google.maps.Rectangle(defaultRectangleOptions);
defaultRectangleOptions 与containRectangleOptions 完全相同,除了边界是 map.getBounds() 并且 visible 设置为 false。
问题是当我单击时看不到矩形,不管我的 alert() 显示了正确的坐标并且也正确地说 containsRectangle.getVisible() 是真的。我也尝试让 containsRectangle 在 defaultRectangleOptions 中可见,但它仍然没有出现。我也有一些几乎相同的代码来显示我单击的圆圈,并且工作完全正确,但矩形永远不会出现。
【问题讨论】:
-
您可以链接到实时代码或将您的代码放在 JS Fiddle 上吗?会更容易排除故障。