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

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

问题描述

我正在从 v2 切换到 v3 谷歌地图 API 并且遇到了 gMap.getBounds() 函数的问题.

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.

这是我的 javascript 代码:

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

所以现在它提醒我 gMap.getBounds() 是未定义的.

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

我尝试在 click 事件中获取 getBounds 值,它对我来说效果很好,但我无法在加载地图事件中获得相同的结果.

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.

在 Google Maps API v2 中加载文档时,getBounds 也能正常工作,但在 V3 中失败.

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?

推荐答案

在 v3 API 的早期,getBounds() 方法要求地图图块加载完毕才能返回正确的结果.但是现在看来您可以监听 bounds_changed 事件,该事件甚至在 tilesloaded 事件之前就被触发:

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天全站免登陆