gpt4 book ai didi

javascript - 为什么我无法使用 getBounds 函数获取纬度和经度?

转载 作者:行者123 更新时间:2023-11-28 00:01:32 24 4
gpt4 key购买 nike

var map;

function initialize(location) {

var currentPosition = new google.maps.LatLng(14.457264, 121.024038);
var mapOptions = {
center: currentPosition,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};

map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions)

var marker = new google.maps.Marker({
position: currentPosition,
map: map,
icon: "../images/02_button_add.png"
});

var bounds = map.getBounds();
alert(bounds);
}

上面是我的代码,我可以在 map 上找到我当前的位置。但是,当尝试使用信息发出警报时,getBounds 函数不起作用,它会给出“未定义”值。

有什么问题吗?

最佳答案

您必须实现一个监听器才能使 getBounds() 正确显示

我建议使用此代码(编辑):

 google.maps.event.addListenerOnce(map, 'idle', function(){

var bounds = map.getBounds();
alert(bounds);
});

这是您的 fiddle ,以便您可以测试它(加载后再次单击运行):http://jsfiddle.net/vA4eQ/280/

关于javascript - 为什么我无法使用 getBounds 函数获取纬度和经度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31759687/

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