簡體   English   中英

zoom_changed 監聽器對我不起作用

[英]zoom_changed listener doesn't work for me

我正在嘗試在谷歌地圖中使用縮放偵聽器事件,以便我可以調整其中的某些軸的大小。 首先,我嘗試使用警報,以便我可以查看偵聽器是否檢測到縮放級別的變化,但我根本無法收到任何警報。 我的代碼如下:

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

有沒有人看到問題?

map變量在您創建 zoom_changed 偵聽器時未定義,它是稍后在 initialize 函數運行時創建的(變量也是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 }
 <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script> <div style="text-align:center;"> <div id="log"></div> </div> <div id="map_canvas" style="width: 100%; height: 90%;"></div>

您需要在initialize()函數內的google.maps.Map實例上設置zoom_changed事件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM