在映射空闲时调用maps.getBounds()为null
maps.getBounds() null when calling on map idle
我需要在a)映射初始化时和b)映射移动时设置指向映射的指针。为了获得标记,我对我的API调用如下:
var bounds = map.getBounds();
然后获取边界以获得正确的标记。
问题是,当我把那个API-call放进的时候
google.maps.event.addListener(map, "bounds_changed", function(){})
它在拖动地图时被调用得太频繁了,但如果我使用
google.maps.event.addListener(map, "idle", function(){})
map.getBounds在初始化时为null。
编辑:更多代码
$(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(); }
界限为空!
我在这里使用这段代码:http://boascervejas.com.br它工作正常:
var mapgoogle = null;
//do some stuff
var mapOptions = //something here
mapgoogle = new google.maps.Map(document.getElementById("map"), mapOptions);
google.maps.event.addListener(mapgoogle, 'idle', function() {
var bounds = mapgoogle.getBounds();
console.log(bounds);
//it's only called when the "end of the drag" is fired
});
EDIT:jsfiddle的完整工作示例(http://jsfiddle.net/pjhk3Lm9/):
js:
var map = null;
function initialize(lat, lng) {
var myLatlng = new google.maps.LatLng(lat,lng);
var mapOptions = {
mapTypeControl: false,
streetViewControl: false,
center: myLatlng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
google.maps.event.addListener(map, 'idle', function() {
console.log(map.getBounds());
})
};
navigator.geolocation.getCurrentPosition(
function(position) {
initialize(position.coords.latitude, position.coords.longitude);
},
function(position) {
initialize(-23.5354986,-46.6839113);
}
);
//html and css
<div id="map-canvas"></div>
#map-canvas {
width: 400px;
height: 400px;
border:1px solid #FF0000;
}
相关文章:
- 数组在递归方法中设置为null
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 无法获取属性'selectedIndex'的未定义引用或null引用
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 来自文本输入null的html javascript变量
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- php代码在textbox更改事件上显示null
- 相同的RegExp返回不同的结果-第一次是正确的结果,第二次是null
- 在php中提交数据时,如果某些值为null,而某些值为非null,如何进行查询
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- 为什么localStorage[“..”]未定义,而localStorage.getItem(“..”)为null
- ajax在输入等于null时进行检查
- html+javascript:如何在某些条件下禁用dropdownlist中的null选项
- 未捕获的类型错误:无法读取属性'状态'在react中为null
- contentWindow.document.body is null
- 在经过DATE验证的列中创建null值
- 为什么是文档.旧版应用程序中的DOM-object.properties为null
- if(foo!==null)的计算结果为true,即使foo为null
- 在映射空闲时调用maps.getBounds()为null
- 为什么movieClip.getBounds()返回null