Google Maps v3:无法获取属性“setMap”的值

时间:2012-10-17 18:24:53

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

我是谷歌地图新手。我将一个Google Maps v3地图添加到ASP.NET 4.0项目中的aspx文件中,并尝试为地图上的四个KML图层添加切换复选框。

但是当我点击复选框时,我收到此错误:“Microsoft JScript运行时错误:无法获取属性'setMap'的值:object is null或undefined”。谢谢!

这是HTML:

<body onload="initialize()">    
        <div id="map-canvas" style="width:100%; height:90%;"></div>
        <div id="legend">
            <div class="column">
                <input type="checkbox" id="landmarksLayer" onclick="toggleKMLLayer(this,'Notable Locations');" checked="checked" />Notable Locations (<img alt="yellow dot" src="Images/GoogleMaps/yellow-dot.png" />)
            </div>
        </div>
    </body>

这是Javascript:

    <%--Javascript--%>
    <script type="text/JavaScript" src="http://maps.googleapis.com/maps/api/js?key=[I_Removed_My_Key]&sensor=false"></script>

    <script type="text/javascript">

        var infowindow = new google.maps.InfoWindow({ "maxWidth": 100 });

        var map;

        var landmarksLayer;
        var publicArtLayer;
        var blueEmergencyPhonesLayer;
        var buildingsLayer;

        //toggle
        function toggleKMLLayer(chkbox, kmlLayerID) {

            //window.alert(kmlLayerID);

            if (chkbox.checked) {
                //window.alert("checked");
                landmarksLayer.setMap(map);
            }
            else {
                //window.alert("unchecked");
                landmarksLayer.setMap(null);
            }
        }

        function initialize() {

            var mapOptions = {
                center: new google.maps.LatLng(33.585737, -101.884804),
                zoom: 15,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

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

            var kmlLayerOptions = {
                preserveViewport: true,
                suppressInfoWindows: true
            };

            var landmarksLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/landmarks.xml', kmlLayerOptions);
            landmarksLayer.setMap(map);

            var publicArtLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/art.xml', kmlLayerOptions);
            publicArtLayer.setMap(map);

            var blueEmergencyPhonesLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/emergency.xml', kmlLayerOptions);
            blueEmergencyPhonesLayer.setMap(map);

            var buildingsLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/bldgs.xml', kmlLayerOptions);
            buildingsLayer.setMap(map);


            addKmlClickHandler(landmarksLayer);
            addKmlClickHandler(publicArtLayer);
            addKmlClickHandler(blueEmergencyPhonesLayer);
            addKmlClickHandler(buildingsLayer);
        }

        // create a new info window for the KML (outage) layer and the geo-coded house marker
        function addKmlClickHandler(KmlLayer) {
            google.maps.event.addListener(KmlLayer, "click", function (event) {
                infowindow.close();
                infowindow.setOptions({
                    pixelOffset: event.pixelOffset,
                    content: event.featureData.infoWindowHtml,
                    position: event.latLng
                });
                infowindow.open(map);
            });
        }

    </script>

1 个答案:

答案 0 :(得分:2)

您正在为图层创建全局变量:

    var landmarksLayer;
    var publicArtLayer;
    var blueEmergencyPhonesLayer;
    var buildingsLayer;

但你没有使用它们(在你的初始化函数中):

    var landmarksLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/landmarks.xml', kmlLayerOptions);
    landmarksLayer.setMap(map);

    var publicArtLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/art.xml', kmlLayerOptions);
    publicArtLayer.setMap(map);

    var blueEmergencyPhonesLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/emergency.xml', kmlLayerOptions);
    blueEmergencyPhonesLayer.setMap(map);

    var buildingsLayer = new google.maps.KmlLayer('http://www.ttu.edu/map/points/bldgs.xml', kmlLayerOptions);
    buildingsLayer.setMap(map);

变量前面的“var”会创建一个新版本的变量,该变量是初始化函数的本地变量,使全局版本保持未初始化状态。您需要初始化全局版本,以便可以在HTML单击侦听器中使用它(在全局上下文中执行)。