【问题标题】:Javascript insists it's not defined, it totally isJavascript坚持它没有定义,它完全是
【发布时间】:2009-07-11 21:03:22
【问题描述】:

老实说,我不习惯编写 JS,更糟糕的是,我正在使用 Google Maps API,虽然有据可查,但它是一个熊。因此,我编写了一个函数,允许用户从链接放大地图。但是解释器坚持我的函数在我调用它时没有定义。该函数是“zoomTo”,它出现在以下怪物脚本中。

function load() {
    if (GBrowserIsCompatible()) {
        var gmarkers = [];
        var htmls = [];
        var i = 0;
        // Read the data
        //++++++++++++++++
        GDownloadUrl("/assets/data/nolag.xml", function(data) {
                var xml = GXml.parse(data);
                var markers = 

        xml.documentElement.getElementsByTagName("marker");

        // Draw icons
        for (var i = 0; i < markers.length; i++) {
            var locoName = markers[i].getAttribute("locoName");
            var speed = markers[i].getAttribute("speed");
            var ip = markers[i].getAttribute("ip");
            var date = markers[i].getAttribute("captureTime");
            var lat = markers[i].getAttribute("lat");
            var lng = markers[i].getAttribute("lng");
            var location = markers[i].getAttribute("location");
            var heading = markers[i].getAttribute("heading");
            var type = markers[i].getAttribute("type");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                   parseFloat(markers[i].getAttribute("lng")));
            var marker = createMarker(point, locoName, speed, ip, date, lat, lng, location, heading, type);
            map.addOverlay(marker);
            }
        });


        // create the map
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl3D());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(40.5500, -72.1700), 7);
        map.enableScrollWheelZoom(); 
        NEC = new GPolyline(NECRoute, "#0d004c", 3, 0.7);
        map.addOverlay(NEC);

    // A function to create the marker and set up the event window
    //+++++++++++++++++

    function createMarker(point, locoName, speed, ip, date, lat, lng, location, heading, type) {
            var marker = new GMarker(point, customIcons[type]);
            marker.tooltip = '<div class="tooltip"><h1>' + locoName + '</h1><h2>' + speed + '<br/>' + location + '</h2></div>';
            marker.contextmenu = '<div class="contextmenu">Hello world</div>';
            var satellite = "<img src=\"./images/icons/satellite.gif\">";
            var gps = "<img src=\"./images/icons/gps.gif\">";
            var cmu = "<img src=\"./images/icons/cmu.gif\">";
            var ftp = "<img src=\"./images/icons/ftp.gif\">";
            var me1k = "<img src=\"./images/icons/me1k.gif\">";
            var html = "<div class=\"bubble\">";
                html += "<h3>" + locoName + "<span class=\"small-data\"> Route 2150</span></h3>";;
                html += "<div class=\"toolbar\">" + gps + satellite + cmu + ftp + me1k + "</div>";
                html += "<h4>Heading " + heading + " @ " + speed + " MPH</h4>"
                html += "<h4>Lat: " + lat + "</h4><h4> Lng: " + lng + "</h4>";
                html += "<h4>IP: " + ip + "</h4>";
                html += "<h4><div class=\"sm-button\"><a style='color: #FFFFFF; decoration:none;' href='map_detail.php'>Details</a></div><div class=\"sm-button\">Zoom To</div></h4>";
                html += "</div>";
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(html);
        });
        gmarkers[i] = marker;
        htmls[i] = html;
        i++;

        //  ======  The new marker "mouseover" and "mouseout" listeners  ======
        GEvent.addListener(marker,"mouseover", function() {
            showTooltip(marker);
        });        
        GEvent.addListener(marker,"mouseout", function() {
            tooltip.style.visibility="hidden"
        }); 
        return marker;
        } 

        // ====== This function displays the tooltip ======
        //+++++++++++++++++++++++++++++++++++++++++++++++++++
        function showTooltip(marker) {
            tooltip.innerHTML = marker.tooltip;
            var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
            var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
            var anchor=marker.getIcon().iconAnchor;
            var width=marker.getIcon().iconSize.width;
            var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
            pos.apply(tooltip);
            tooltip.style.visibility="visible";
        }


        // ===== This function is invoked when the mouse goes over an entry in the side_bar =====
        function mymouseover(i) {
            showTooltip(gmarkers[i])
        }
        // ===== This function is invoked when the mouse leaves an entry in the side_bar =====
        function mymouseout() {
            tooltip.style.visibility="hidden";
        }

        // This function picks up the side_bar click and opens the corresponding info window
        function myclick(i) {
            gmarkers[i].openInfoWindowHtml(htmls[i]);
        }




        // ====== set up marker mouseover tooltip div ======
        var tooltip = document.createElement("div");
        document.getElementById("map").appendChild(tooltip);
        tooltip.style.visibility="hidden";


        // === create the context menu div ===
        var contextmenu = document.createElement("div");
        contextmenu.style.visibility="hidden";


        // === functions that perform the context menu options ===
        function zoomTo( lat, lng, level) {
            // perform the requested operation
            map.setCenter( new GLatLng(lat,lng), level);
            // hide the context menu now that it has been used
            contextmenu.style.visibility="hidden";
        }


        contextmenu.innerHTML = '<div class="context"><ul>'
        + '<li class="sectionheading"><a href="javascript:map.setCenter(new GLatLng(40.5500, -72.1700), 7)">Zoom Out</a></li>'
        + '<li class="sectionheading"><a href="javascript:zoomTo(42.343193, -71.049442, 14)">Boston Area</a></li>'
        + '<li><a href="javascript:zoomTo(42.351662,-71.054978,17)">South Station, Boston</a></li>'
        + '<li><a href="javascript:zoomTo(42.333161,-71.060096,17)">South Hampton</a></li>'
        + '<li><a href="javascript:zoomTo(41.293422,-72.928587,17)">New Haven</a></li>'
        + '<li class="sectionheading"><a href="javascript:zoomTo(40.745696, -74.019699, 12)">New York Area</a></li>'
        + '<li><a href="javascript:zoomTo(40.749061,-73.930553,17)">Sunny Side</a></li>'
        + '<li><a href="javascript:zoomTo(40.752556,-73.998113,17)">NY, Penn Station</a></li>'
        + '<li><a href="javascript:zoomTo(39.957517,-75.181376,17)">30th Street, Philadelphia</a></li>'
        + '<li><a href="javascript:zoomTo(39.746481,-75.522090,17)">Wilmington Shops</a></li>'
        + '<li><a href="javascript:zoomTo(39.307817,-76.615412,17)">Baltimore, Penn Station</a></li>'
        + '<li class="sectionheading"><a href="javascript:zoomTo(38.912675, -76.992874, 14)">DC Area</a></li>'
        + '<li><a href="javascript:zoomTo(38.916531,-76.987703,17)">DC, Ivy City</a></li>'
        + '<li><a href="javascript:zoomTo(38.900210,-77.005266,17)">DC, Union Station</a></li>'
        + '</ul</div>'; 

        map.getContainer().appendChild(contextmenu);


        // === listen for singlerightclick ===
        GEvent.addListener(map,"singlerightclick",function(pixel,tile) {
            clickedPixel = pixel;
            var x=pixel.x;
            var y=pixel.y;
            if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }
            if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }
            var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));
            pos.apply(contextmenu);
            contextmenu.style.visibility = "visible";
        });
        // === If the user clicks on the map, close the context menu ===
        GEvent.addListener(map, "click", function() {
            contextmenu.style.visibility="hidden";
        });
    } else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

    zoomTo 似乎是顶级函数 load() 中的一个匿名函数 - 因此,它不适用于顶级调用。

    代替

    // === functions that perform the context menu options ===
                    function zoomTo( lat, lng, level) {
                            // perform the requested operation
                            map.setCenter( new GLatLng(lat,lng), level);
                            // hide the context menu now that it has been used
                            contextmenu.style.visibility="hidden";
                    }
    

    把函数放在顶层,传入map和contextmenu变量:

    // === functions that perform the context menu options ===
    function zoomTo( lat, lng, level) {
    // perform the requested operation
                            map.setCenter( new GLatLng(lat,lng), level);
                            // hide the context menu now that it has been used
                            contextmenu.style.visibility="hidden";
                    }
    

    【讨论】:

    • 就是这样写 跨度>
    • 这是个好主意。然而,我试过了,没有得到爱。这一次,不是 zoomTo 未定义,而是 map 未定义。我现在要睡觉了,因为我不能再看它了。非常感谢更多回复。干杯!
    • 无需完全重写您的 JS ;) 将 map 变量也放在顶层。所以你有 var map = null;当你初始化你的地图变量时,不要把 var 放在它前面,因为它,嗯,我猜已经是一个“全局”变量了。 map = new GMap2(document.getElementById("map"));
    【解决方案2】:

    因为它在评论中看起来不太好。再次在这里发表上面的评论

    <html> 
      <head> 
        <script type="text/javascript"> 
          function zoomTo(x,y,z) { 
            .... 
          } 
        </script> 
      .....
      </head>
      ......
     </html>
    

    【讨论】:

      猜你喜欢
      • 2016-08-09
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 2018-11-09
      • 1970-01-01
      • 2021-07-20
      • 2016-05-08
      • 1970-01-01
      相关资源
      最近更新 更多