0

我正在使用 Google Maps Javascript API。

每次用户搜索某个地方时,我都会在地图中显示每个地方的标记,并fitBounds用于设置视口以包含给定的边界。

400px width因为我在右侧的地图上方有一个面板,所以我400pxfitBounds.

var bounds = new google.maps.LatLngBounds();
bounds.extend(new google.maps.LatLng(lat, lng)); //this line is executed inside a loop for every place
map.fitBounds(bounds, {right: 400});

当我在边界内有多个点时,fitBounds应用填充效果很好。但是当我在边界内只有一个点时,fitBounds不会应用填充。取而代之的是它的工作原理panTo,只是更改了地图的中心。

这是 Google Maps API 中的错误吗?有什么解决方法吗?

4

1 回答 1

1

这似乎是 API 上的错误,您可以在此处报告。

该问题的一种解决方法是在调用该方法后只有一个点时使用panByfitBounds方法,就像在这个fiddle中一样。

var bounds = new google.maps.LatLngBounds();
bounds.extend(new google.maps.LatLng(lat, lng)); //this line is executed inside a loop for every place
map.fitBounds(bounds, {right: 400});
if (pointsCount == 1) { // initialize the var and use the loop to count
    map.panBy(200, 0); // has to be half of needed padding
}
于 2019-08-16T13:41:44.853 回答