我试图做到这一点,以便当用户单击地图上的一个大头针时,它会以该大头针为中心,但如果信息框太高并且偏离视图,那么它将重新调整以偏移地图上的部分。这是代码,但似乎并非每次都能正常工作。某些信息框仍在屏幕外流动,并且 dy 正在向 0 发出警报。所以这里是代码:
//Displaying and hiding infobox
function displayInfobox(e) {
if (e.targetType == 'pushpin') {
infobox.setLocation(e.target.getLocation());
infobox.setOptions({
visible: true,
title: e.target.Title,
description: e.target.htmlContent,
width: 250,
offset: new MM.Point(-3, 0)
});
//centering map on pushpin
var pinLocation = e.target.getLocation();
map.setView({
center: pinLocation
});
infoBoxOrigHeight = $('.Infobox').outerHeight();
infoBoxHeight = $('.Infobox .infobox-body').outerHeight();
var stalkHeight = $('.infobox-stalk').outerHeight();
var newOffset = (infoBoxHeight - infoBoxOrigHeight) + stalkHeight;
$('.Infobox').css({
'height': infoBoxHeight,
'top': -newOffset
});
$('.Infobox').css('height', infoBoxHeight);
$('.infobox-stalk').css('top', infoBoxHeight);
var buffer = 100;
var infoboxOffset = infobox.getOffset();
var infoboxAnchor = infobox.getAnchor();
var infoboxLocation = map.tryLocationToPixel(e.target.getLocation(), Microsoft.Maps.PixelReference.control);
var dx = infoboxLocation.x + infoboxOffset.x - infoboxAnchor.x;
var dy = infoboxLocation.y - 100 - infoboxAnchor.y;
if (dy < buffer) { //Infobox overlaps with top of map.
//Offset in opposite direction.
dy *= -1;
//add buffer from the top edge of the map.
dy += buffer;
} else {
//If dy is greater than zero than it does not overlap.
dy = 0;
}
if (dx < buffer) { //Check to see if overlapping with left side of map.
//Offset in opposite direction.
dx *= -1;
//add a buffer from the left edge of the map.
dx += buffer;
} else { //Check to see if overlapping with right side of map.
dx = map.getWidth() - infoboxLocation.x + infoboxAnchor.x - infobox.getWidth();
//If dx is greater than zero then it does not overlap.
if (dx > buffer) {
dx = 0;
} else {
//add a buffer from the right edge of the map.
dx -= buffer;
}
}
//Adjust the map so infobox is in view
alert('dx: ' + dx + ' dy: ' + dy);
if (dx != 0 || dy != 0) {
map.setView({
centerOffset: new Microsoft.Maps.Point(dx, dy),
center: map.getCenter()
});
}
}
}
只是想知道我在执行什么错误,这不能正常工作?