Google Maps Api v3 - getBounds未定义 [英] Google Maps Api v3 - getBounds is undefined

查看:80
本文介绍了Google Maps Api v3 - getBounds未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



$ p
$ b

这是我的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()未定义。



我试图在click事件中获取getBounds值,它对我来说工作正常,但我无法在加载地图事件中获得相同的结果。 b
$ b

在Google Maps API v2中加载文档时,getBounds也能正常工作,但在V3中无法使用。



请帮助我解决这个问题?

解决方案

在v3 API的早期阶段, getBounds()方法需要地图图块完成加载才能返回正确的结果。不过现在看来你可以听到 bounds_changed 事件,它甚至在 tilesloaded 事件之前被触发:

 <!DOCTYPE html> 
< html>
< head>
< meta http-equiv =content-typecontent =text / html; charset = UTF-8/>
< title> Google Maps v3 - getBounds未定义< / title>
< script src =http://maps.google.com/maps/api/js?sensor=false
type =text / javascript>< / script>
< / head>
< body>
< div id =mapstyle =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>


I'm switching from v2 to v3 google maps api and got a problem with gMap.getBounds() function.

I need to get the bounds of my map after its initialization.

Here is my javascript code:


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());
    }
);

So now it alerts me that gMap.getBounds() is undefined.

I've tried to get getBounds values in click event and it works fine for me, but I cannot get the same results in load map event.

Also getBounds works fine while document is loading in Google Maps API v2, but it fails in V3.

Could you please help me to solve this problem?

解决方案

In the early days of the v3 API, the getBounds() method required the map tiles to have finished loading for it to return correct results. However now it seems that you can listen to bounds_changed event, which is fired even before the tilesloaded event:

<!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>

这篇关于Google Maps Api v3 - getBounds未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆