geoxml v3在使用多个KML文件时突出显示多边形

时间:2015-10-09 11:02:41

标签: javascript google-maps geoxml3 geoxml

由于需要葡萄牙地图,用户可以选择地区点击,我跟着this examplethis one。问题是我使用每个区域的KML文件绘制为多边形,而不是使用KML用于所有区域多边形。

此处已经搜索了很多问题,并尝试更改useTheData()回调函数以包含一个额外的循环,如下所示,但没有成功......

    function useTheData(doc) {

        var sidebarHtml = '<table><tr><td><a href="javascript:showAll();">Show All</a></td></tr>';

        for (var j = 0; j < doc.length ; j++) { 
          ...
        }
        sidebarHtml += "</table>";

        document.getElementById("map_barralateral").innerHTML = sidebarHtml;
    };

地图上的悬停区域工作正常,但是当单击侧栏上的高亮显示链接时,它默认突出显示使用列表的最后一个KML文件创建的多边形...

有没有解决这个问题?

我的完整代码在这里:http://afonsogomes.com/mapas

<!DOCTYPE html>
<html lang="pt">

<head>
    <meta charset="utf-8">
    <title>XXXXX</title>
    <style>
    html,body {margin: 0;padding: 0;}
    #map_canvas, #map_barralateral {width: 300px;height: 500px;margin: 0;padding: 0;float: left;background-color: #e5e3df;}
    #map_canvas{height: 520px;}
    #map_barralateral{width: 300px;font-family: Arial, sans-serif;font-size: 12px;padding: 10px;}
    #loaddiv {width: 620px;background-color: #78A04C;border: none;padding: 20px 0;font-family: Arial, sans-serif;color: #FFF;font-weight: 700;text-align: center;position: absolute;left: 0;top: 200px;margin: 0 auto;filter: alpha(opacity=90);opacity: .90;z-index: 100;}
    </style>
    <script src="../js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript" src="http://geoxml3.googlecode.com/svn/branches/polys/geoxml3.js"></script>
    <script type="text/javascript" src="http://geoxml3.googlecode.com/svn/trunk/ProjectedOverlay.js"></script>

    <script>
        var geoXml = null;
        var geoXmlDoc = null;
        var map = null;
        var myLatLng = null;
        var myGeoXml3Zoom = true;
        var sidebarHtml = "";

        var filename_example = ["kml/porto.kml", "kml/faro.kml", "kml/braganca.kml", "kml/evora.kml"];
        function initialize() {
           myLatLng = new google.maps.LatLng(39.96293, -8.03770);

            var myOptions = {
                zoom: 18,
                center: myLatLng,

                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("map_canvas"),
                myOptions);

            showLoad();

            geoXml = new geoXML3.parser({
                map: map,
                zoom: myGeoXml3Zoom,
                suppressInfoWindows: true,
                singleInfoWindow: false,
                afterParse: useTheData
            });
            google.maps.event.addListener(geoXml, 'parsed', function () {
                hideLoad();
            });

            geoXml.parse(filename_example);
        };

        function kmlHighlightPoly(poly) {
            for (var i = 0; i < geoXmlDoc.gpolygons.length; i++) {
                if (i == poly) {
                    geoXmlDoc.gpolygons[i].setOptions({ fillColor: "#0000FF", strokeColor: "#0000FF" });
                } else {
                    geoXmlDoc.gpolygons[i].setOptions({ fillColor: "#FF0000", strokeColor: "#FF0000" });
                }
            }
        }

        function showAll() {
            map.fitBounds(geoXmlDoc.bounds);
            for (var i = 0; i < geoXmlDoc.gpolygons.length; i++) {
                geoXmlDoc.gpolygons[i].setMap(map);
            }
        }

        function highlightPoly(poly) {
            google.maps.event.addListener(poly, "mouseover", function () {
                poly.setOptions({ fillColor: "#FF0000", strokeColor: "#FF0000" });
            });
            google.maps.event.addListener(poly, "mouseout", function () {
                poly.setOptions({ fillColor: "#78A04C", strokeColor: "#78A04C", fillOpacity: 0.4 });
            });
        }

        function useTheData(doc) {

            var sidebarHtml = '<table><tr><td><a href="javascript:showAll();">Show All</a></td></tr>';

            for (var j = 0; j < doc.length ; j++) { // Added by me to parse all kml files to the map

                geoXmlDoc = doc[j];
                for (var i = 0; i < doc[j].gpolygons.length; i++) {
                    sidebarHtml += '<tr><td>' + doc[j].placemarks[i].name + ' - <a   href="javascript:kmlHighlightPoly(' + j + ');">highlight</a><br></td></tr>';
                    highlightPoly(doc[j].gpolygons[i]);
                }
            }
            sidebarHtml += "</table>";

            document.getElementById("map_barralateral").innerHTML = sidebarHtml;
        };

        function hideLoad() {
            var loaddiv = document.getElementById("loaddiv");
            if (loaddiv == null) {
                alert("Sorry can't find the loaddiv");
                return;
            }
            loaddiv.style.visibility = "hidden";
        }

        function showLoad() {
            var loaddiv = document.getElementById("loaddiv");
            if (loaddiv == null) {
                alert("Sorry can't find your loaddiv");
                return;
            }
            loaddiv.style.visibility = "visible";
        }
    </script>
</head>

<body onload="initialize()">
    <div id="loaddiv">A carregar.....&#160;&#160;&#160; Por favor aguarde!</div>
    <div id="map_canvas"></div>
    <div id="map_barralateral"></div>
    <div id="map_status"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您还需要修改kmlHighlightPoly以考虑使用多个KML文件:

function kmlHighlightPoly(doc,poly) {
  for (var j=0; j < geoXmlDoc.length; j++) {
    for (var i = 0; i < geoXmlDoc[doc].gpolygons.length; i++) {
        if ((j == doc) && (i == poly)) {
            geoXmlDoc[j].gpolygons[i].setOptions({ fillColor: "#FF0000", strokeColor: "#FF0000" });
        } else {
            geoXmlDoc[j].gpolygons[i].setOptions({ fillColor: "#78A04C", strokeColor: "#78A04C", fillOpacity: 0.4 });
        }
    }
  }
}

并更改useTheData以将文档传递给kmlHighlightPoly函数。

function useTheData(doc) {
    var sidebarHtml = '<table><tr><td><a href="javascript:showAll();">Show All</a></td></tr>';

    for (var j = 0; j < doc.length ; j++) { // Added by me to parse all kml files to the map

        geoXmlDoc = doc;
        for (var i = 0; i < doc[j].gpolygons.length; i++) {
            sidebarHtml += '<tr><td>' + doc[j].placemarks[i].name + ' - <a   href="javascript:kmlHighlightPoly('+j+','+i+');">highlight</a><br></td></tr>';
            highlightPoly(doc[j].gpolygons[i]);
        }
    }
    sidebarHtml += "</table>";
    document.getElementById("map_barralateral").innerHTML = sidebarHtml;
};

working example