zoom_changed listener对我不起作用

时间:2016-09-08 16:39:02

标签: javascript html google-maps google-maps-api-3

我试图在谷歌地图中使用缩放监听器事件,以便我可以调整其中的某个轴的大小。作为一个开始,我尝试使用警报,以便我可以看到监听器是否检测到缩放级别的变化,但我根本无法获得任何警报。我的代码如下:

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps - pygmaps </title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=visualization&sensor=true_or_false"></script>
<script type="text/javascript">
    var map;
    function initialize() {
        var centerlatlng = new google.maps.LatLng(49.801674, 11.188139);
        var myOptions = {
            zoom: 18,
            center: centerlatlng,
            scaleControl:true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

*密谋*

google.maps.event.addDomListener(window, 'load',initialize);
google.maps.event.addDomListener(maps,'zoom_changed', function()
{
    alert("changed");
});

</script>
</head>
<body>

    <div style="text-align:center;">
        <button onclick="draw_axis()">Enable/Disable Axis</button> <br><br>    <div id="log"></div>  
    </div>
    <div id="map_canvas" style="width: 100%; height: 90%;"></div>
    </body>
    </html>

有没有人看到这个问题?

2 个答案:

答案 0 :(得分:2)

创建zoom_changed侦听器时未定义$d2 = $dl->src; $test = explode("/", $d2); $test[8]; 变量,稍后会在初始化函数运行时创建(变量为map,而不是map)。

代码段

maps
var map;

function initialize() {
  var centerlatlng = new google.maps.LatLng(49.801674, 11.188139);
  var myOptions = {
    zoom: 18,
    center: centerlatlng,
    scaleControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  google.maps.event.addDomListener(map, 'zoom_changed', function() {
    alert("changed");
  });

}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}

答案 1 :(得分:1)

您需要在zoom_changed函数内的google.maps.Map实例上设置initialize()个事件。

map.addListener('zoom_changed', function() {
    alert("changed");
});