【问题标题】:how to check the Variable is or not in the Memory using javascript如何使用javascript检查变量是否在内存中
【发布时间】:2010-10-19 10:41:56
【问题描述】:

当我放大地图但看不到标记时,我想检查标记(谷歌地图 v3)是否在内存中。

那我该怎么办,

【问题讨论】:

    标签: javascript google-maps memory google-maps-markers


    【解决方案1】:

    您需要以数组的形式跟踪添加到地图的标记。您需要添加一个 bounds_changed 事件侦听器,其中包含一个搜索该数组并检查标记坐标是否在地图范围内的函数。例如:

    <html>
        <head>
            <script type="text/javascript" src= "http://maps.google.com/maps/api/js?sensor=false">
            </script>
            <script>
    
                var map;
                markers = [];
                function initialize(){
                    geocoder = new google.maps.Geocoder();
                    var latlng = new google.maps.LatLng(-34.397, 150.644);
                    var myOptions = {
                        zoom: 8,
                        center: latlng,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    }
                    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
                    //if creating multiple markers you would do marker creation and storage in a loop
                    var marker1 = new google.maps.Marker({
                        map: map,
                        position: latlng,
                        title: "Marker 1"
                    });
                    var marker2 = new google.maps.Marker({
                        map: map,
                        position: new google.maps.LatLng(-34.2, 150.644),
                        title: "Marker 2"
                    });
                    //save your marker to an array to keep track of your markers - i am assuming all markers are initially visible
                    markers.push({
                        visible: true,
                        marker: marker1
                    })
                    markers.push({
                        visible: true,
                        marker: marker2
                    })
    
                    //watch for zoom change event
                    google.maps.event.addListener(map, 'bounds_changed', function(){
                        checkBounds()
    
                    })
    
                }
    
                function checkBounds(){
    
                    //get bounds for a new zoom level
                    bounds = map.getBounds();
                    //loop through markers array to check if they are still in bounds (visible)
                    for (i = 0; i < markers.length; i++) {
                       m = markers[i].marker
                        if (bounds.contains(m.position)) {
                            //do something here for markers that are visible
                            markers[i].visible = true;
                        }
                        else {
                            //do something here for markers that are not visible
                            markers[i].visible = false;
                        }
                    }
                }
    
                //this is just to show which markers are visible and which ones are not
                function show(){
                    htmlVisible = ""
                    htmlHidden = ""
                    for (i = 0; i < markers.length; i++) {
                         m = markers[i].marker
                        if (markers[i].visible) {
    
                            htmlVisible += m.title + "\n";
                        }
                        else {
                            htmlHidden += m.title + "\n";
                        }
    
                    }
                    alert("Visible markers:\n" + htmlVisible + "\n" + "Markers out of bounds:\n" + htmlHidden)
                }
            </script>
        </head>
        <body onload="initialize()">
            <div id="map_canvas" style="width: 320px; height: 480px;">
            </div>
            <input type=button onclick="show()" value="List visible/out of bounds markers">
        </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2011-08-18
      • 1970-01-01
      • 2010-10-25
      • 2014-01-20
      • 1970-01-01
      • 2023-04-02
      • 2014-01-30
      • 2012-07-05
      相关资源
      最近更新 更多