尝试在Google Maps V3中创建InfoWindows时出现奇怪错误

时间:2012-03-20 15:39:47

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

每当我与地图中的标记或折线交互时,我都会收到错误“Uncaught TypeError:Object#没有方法'L'”,该标记或折线具有应该在click事件上打开的InfoWindow。标记InfoWindow打开时出现错误,我无法关闭它,并且InfoWindow根本不会打开我的折线。这是代码:

function downloadUrl(url,callback) {
    var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            request.onreadystatechange = doNothing;
            callback(request, request.status);
        }
    };
    request.open('GET', url, true);
    request.send(null);
}

function doNothing() {}

function decodeLevels(encodedLevelsString) {
    var decodedLevels = [];

    for (var i = 0; i < encodedLevelsString.length; ++i) {
            var level = encodedLevelsString.charCodeAt(i) - 63;
            decodedLevels.push(level);
    }
        return decodedLevels;
}

function createPolyLine(span_id, desc, encoded_span, plevels, pcolor, pweight, popacity) {
    var decodedPath = google.maps.geometry.encoding.decodePath(encoded_span);
    var decodedLevels = decodeLevels(plevels);

    var polyline = new google.maps.Polyline({
            path: decodedPath,
            levels: decodedLevels,
            strokeColor: pcolor,
            strokeOpacity: popacity,
            strokeWeight: pweight,
    });

    // Custom polyline events
    //--------------------------------------------------------
    var html = "[" + span_id + "] " + desc;

    google.maps.event.addListener(polyline, 'click', function(event) {
        var infowindow = new google.maps.InfoWindow({
                content: "infowindow text content"});
        infowindow.position = event.latLng;
        infowindow.open(map);
    });

    //--------------------------------------------------------

    return polyline;
}

function createMarker(point, name, address, clli, type, site_id, site_name) {
    var html = "<h3>" + name + "</h3>" + "<p style='text-align:left;margin-bottom:0px;'>" + address + "<br/>" + clli + "<br/><a class='btn small green' href='view_site.php?site_id=" + site_id + "&site_name=" + site_name + "'>View POP</a></p>";
    var marker = new google.maps.Marker({
        position: point,
        //map: map,
    });


    // Custom marker events
    //--------------------------------------------------------
        google.maps.event.addListener(marker, 'click', function() {
            var infowindow = new google.maps.InfoWindow({
              content: html
            });
        infowindow.open(map,marker);
        });
    //--------------------------------------------------------

    return marker;
}

function initialize()
{
    var infowindow;
    var latlng = new google.maps.LatLng(44.247904,-90.46225);
    var opt =
    { 
        center:latlng,
        scrollwheel: false,
        zoom:8,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableAutoPan:false,
        navigationControl:true,
        navigationControlOptions: {style:google.maps.NavigationControlStyle.ZOOM_PAN },
        mapTypeControl:true,
        mapTypeControlOptions: {style:google.maps.MapTypeControlStyle.DROPDOWN_MENU}
    };

    downloadUrl("gen_span_xml_v3.php", function(data) {
        var xml = data.responseXML;
        var spans = xml.documentElement.getElementsByTagName("span");
        for (var i = 0; i < spans.length; i++) {
            var span_id = spans[i].getAttribute("id");
            var desc = spans[i].getAttribute("desc");
            var encoded_span = spans[i].getAttribute("encoded");
            var levels = spans[i].getAttribute("levels");
            var encodedPolyline = createPolyLine(span_id, desc, encoded_span, levels, '#0055ff', '4', '.5')
            encodedPolyline.setMap(map);
        }
    });

    downloadUrl("gen_marker_xml.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
            var clli = markers[i].getAttribute("clli");
            var type = markers[i].getAttribute("type");
            var site_id = markers[i].getAttribute("site_id");
            var site_name = markers[i].getAttribute("site_name");
            var point = new google.maps.LatLng(
                parseFloat(markers[i].getAttribute("lat")),
                parseFloat(markers[i].getAttribute("lng")));
            var new_marker = createMarker(point, name, address, clli, type, site_id, site_name);
            new_marker.setMap(map);
          }
    });

    var map = new google.maps.Map(document.getElementById("map"),opt);
}

0 个答案:

没有答案