这可以使用 mapspanBy方法实现,此示例将把地图切成两半,然后切成四分之一。新中心将位于地图容器的 3/4 标记处。开发此代码是为了允许地图容器扩大尺寸,但在地图以全尺寸加载但被切断时仍将用户标记居中。
您可以玩弄数字以使您的位置正确。
var map, // Your map object
mapWidth = $('#map').css('width'); // Width of map container
nCentre,
cCentre,
visibleWidth = 200; // Visible area in PX you want map to be centered in
// Make sure width is an Int
mapWidth = parseInt(mapWidth.replace(/px$/, ''), 10);
// Get the center of viewable area
cCentre = visibleWidth / 2;
// Create the new position variable
nCentre = cCentre - (mapWidth / 2);
// Pan the map to the centre of the visible area
if (nCentre !== 0) {
map.panBy(nCentre, 0);
}
对于您的要求,您可能不需要进行第二次计算,因为您实际上只需要计算出可见区域和地图容器的偏移量,然后根据偏移量平移到新中心。