gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:21:17 32 4
gpt4 key购买 nike

需要一张葡萄牙 map ,用户可以在点击时选择地区,我关注了this examplethis one .问题是我为每个要绘制为多边形的地区使用 KML 文件,而不是为所有地区多边形使用 KML。

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

    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;
};

将鼠标悬停在 map 上的地区工作正常,但是当单击侧边栏上的突出显示链接时,它默认突出显示使用列表的最后一个 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>

最佳答案

您还需要修改 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

关于javascript - geoxml v3 在使用多个 KML 文件时突出显示多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33036622/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com