Google地图map.getBounds()在调用map.fitBounds后立即生效 [英] Google Maps map.getBounds() immediately after a call to map.fitBounds
问题描述
重现这个的代码如下:
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Strict // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd >
< html xmlns =http://www.w3.org/1999/xhtmlxmlns:v =urn:schemas-microsoft-com:vml>
< head>
< meta http-equiv =content-typecontent =text / html; charset = utf-8/>
< script type =text / javascriptsrc =http://maps.google.com/maps/api/js?sensor=true>< / script>
< style>
#map {
width:800px;
height:400px;
}
< / style>
< / head>
< body>
< div id ='map'>< / div>
< script>
var myLatlng1 = new google.maps.LatLng(-38.397,150.644);
var myLatlng2 = new google.maps.LatLng(-34.897,150.844);
var myLatLngBounds = new google.maps.LatLngBounds(myLatlng1,myLatlng2);
var myOptions = {
mapTypeId:google.maps.MapTypeId.ROADMAP,
center:new google.maps.LatLng(0,0),
zoom: 0
}
var map = new google.maps.Map(document.getElementById(map),myOptions);
map.fitBounds(myLatLngBounds);
console.log(map.getMapTypeId());
console.log(map.getZoom());
console.log(map.getBounds());
< / script>
< / body>
< / html>
有什么我错过了吗?我一直无法找到有关这种情况的文档中的任何内容。最近我得到的是一个关于getBounds的说明:
如果地图尚未初始化(即mapType仍为空),或中心和缩放未设置,则结果为空。
请注意,getZoom也会返回未定义。 fitBounds()没有设置这个值吗?
编辑
我已经使用默认缩放和居中更新了代码,如根据Marcelo的建议。
您必须在bounds_changed事件后获取边界。
I'm calling getBounds() directly after a call to fitBounds(), and I thought I'd get a valid bound back as the map recenters and zooms to fit the bounds. Unfortunately, getBounds() is returning nil.
The code to reproduce this is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<style>
#map {
width: 800px;
height: 400px;
}
</style>
</head>
<body>
<div id='map'></div>
<script>
var myLatlng1 = new google.maps.LatLng(-38.397, 150.644);
var myLatlng2 = new google.maps.LatLng(-34.897, 150.844);
var myLatLngBounds = new google.maps.LatLngBounds(myLatlng1, myLatlng2);
var myOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(0, 0),
zoom: 0
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
map.fitBounds(myLatLngBounds);
console.log(map.getMapTypeId());
console.log(map.getZoom());
console.log(map.getBounds());
</script>
</body>
</html>
Is there something I'm missing? I haven't been able to find anything in the docs about this situation. The nearest I get is a note on getBounds that says:
If the map is not yet initialized (i.e. the mapType is still null), or center and zoom have not been set then the result is null.
Note that getZoom returns undefined, too. Does fitBounds() not set this value?
EDIT I've updated the code with a default zoom and center, as per Marcelo's suggestions.
You will have to get the bounds after the bounds_changed event.
这篇关于Google地图map.getBounds()在调用map.fitBounds后立即生效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!