gpt4 book ai didi

javascript - Google Maps Api v3 - getBounds 未定义

转载 作者:IT王子 更新时间:2023-10-29 02:46:59 24 4
gpt4 key购买 nike

我正在从 v2 切换到 v3 google maps api,但遇到了 gMap.getBounds() 函数的问题。

我需要在初始化后获取我的 map 的边界。

这是我的javascript代码:


var gMap;
$(document).ready(

function() {

var latlng = new google.maps.LatLng(55.755327, 37.622166);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
gMap = new google.maps.Map(document.getElementById("GoogleMapControl"), myOptions);

alert(gMap.getBounds());
}
);

现在它提醒我 gMap.getBounds() 未定义。

我尝试在点击事件中获取 getBounds 值,它对我来说工作正常,但我无法在加载 map 事件中获得相同的结果。

在 Google Maps API v2 中加载文档时,getBounds 也能正常工作,但在 V3 中失败。

你能帮我解决这个问题吗?

最佳答案

在 v3 API 的早期,getBounds() 方法需要 map 图 block 完成加载才能返回正确的结果。但是现在您似乎可以收听 bounds_changed 事件,该事件甚至在 tilesloaded 事件之前触发:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps v3 - getBounds is undefined</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 350px;"></div>

<script type="text/javascript">
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 12,
center: new google.maps.LatLng(55.755327, 37.622166),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

google.maps.event.addListener(map, 'bounds_changed', function() {
alert(map.getBounds());
});
</script>
</body>
</html>

关于javascript - Google Maps Api v3 - getBounds 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2832636/

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