gpt4 book ai didi

javascript - fitBounds 和 getZoom 表现不佳

转载 作者:行者123 更新时间:2023-11-29 16:27:14 26 4
gpt4 key购买 nike

我有以下代码,在 map 初始化后几乎立即运行

function showAddress(address) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.fitBounds(results[0].geometry.bounds);

if (map.getZoom() < 12) {
map.setZoom(12);
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}

问题是缩放检查和设置不起作用。我已检查并启动了 map ,但此时 map.getZoom() 函数返回未定义。

我可以做些什么来强制它等待,直到设置了拟合边界并且知道缩放级别,以便我可以适本地控制缩放?

最佳答案

  // Define the Bounds Changed event handler to do a "once-only" zoom level check
google.maps.event.addListener(map, 'bounds_changed', function() {
InitialZoomCheck()
});

geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.fitBounds(results[0].geometry.bounds);




function InitialZoomCheck() {

// Re-define the event handler so that this routine is called only once
google.maps.event.addListener(map, 'bounds_changed', function() {
document.posicion.z.value=map.getZoom()
});

if (map.getZoom() > 12) {
map.setZoom (12);
}
}

关于javascript - fitBounds 和 getZoom 表现不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4793765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com