Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么可以';我不能使用getBounds函数获取纬度和经度吗?_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 为什么可以';我不能使用getBounds函数获取纬度和经度吗?

Javascript 为什么可以';我不能使用getBounds函数获取纬度和经度吗?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,以上是我的代码,我可以在地图上找到我的当前位置。 但是,getBounds函数在尝试使用信息发出警报时不起作用,因此会给出“undefined”值 有什么问题吗?您必须实现一个侦听器才能正确显示getBounds() 我建议此代码(编辑): var map; function initialize(location) { var currentPosition = new google.maps.LatLng(14.457264, 121.024038); var mapOptions =

以上是我的代码,我可以在地图上找到我的当前位置。 但是,getBounds函数在尝试使用信息发出警报时不起作用,因此会给出“undefined”值


有什么问题吗?

您必须实现一个侦听器才能正确显示getBounds()

我建议此代码(编辑)

var map;

function initialize(location) {

var currentPosition = new google.maps.LatLng(14.457264, 121.024038);
var mapOptions = {
  center: currentPosition,
  zoom: 12,
  mapTypeId: google.maps.MapTypeId.ROADMAP,
};

map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions)

var marker = new google.maps.Marker({
  position: currentPosition,
  map: map,
  icon: "../images/02_button_add.png"
});

var bounds = map.getBounds();
alert(bounds);
}

这是您的小提琴,您可以测试它(加载后再次单击run):

根据谷歌文档

getBounds()

返回值:LatLngBounds 返回当前视口的横向/横向边界。如果可以看到多个世界副本,则边界的经度范围为-180到180度(含180度)。如果贴图尚未初始化(即贴图类型仍然为空),或未设置“中心”和“缩放”,则结果为空或未定义


因此,您必须首先初始化地图。

谢谢。但是如何在地图初始化后获得边界而不改变边界?重复
 google.maps.event.addListenerOnce(map, 'idle', function(){

   var bounds = map.getBounds();
   alert(bounds);
});