【发布时间】:2015-05-19 20:38:00
【问题描述】:
我需要设置指向地图的指针 a) 初始化地图时和 b) 移动地图时。 为了获得标记,我像这样调用我的 API:
var bounds = map.getBounds();
然后获取边界以获得正确的标记。
问题是,当我将 API 调用放入其中时
google.maps.event.addListener(map, "bounds_changed", function(){})
在拖动地图时它被调用的频率太高了,但是如果我使用
google.maps.event.addListener(map, "idle", function(){})
map.getBounds 在初始化时为空。
编辑:更多代码
$(document).ready(function(){ initialize(); });
function initialize() { mapOptions = {
zoom : 13,
minZoom : minimumZoomLevel,
scrollwheel : false,
panControl : false }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
google.maps.event.addListener(map, "idle", function() {
myfunction(); }); });
function myfunction(){ var bounds = map.getBounds(); }
bounds 为空!
【问题讨论】:
-
你能显示一些代码吗?也许在 jsffidle 中?
-
bounds_changed 事件呢? stackoverflow.com/questions/2832636/…
-
我在原始帖子中对此发表了评论,拖动时触发太频繁(更好:拖动时!)
-
我在这里做了这个小提琴:jsfiddle.net/pjhk3Lm9 它工作正常。 map 是否定义为全局变量?在 myFunction() 范围内是否可以访问?