作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
需要一张葡萄牙 map ,用户可以在点击时选择地区,我关注了this example和 this 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.....    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;
};
关于javascript - geoxml v3 在使用多个 KML 文件时突出显示多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33036622/
需要一张葡萄牙 map ,用户可以在点击时选择地区,我关注了this example和 this one .问题是我为每个要绘制为多边形的地区使用 KML 文件,而不是为所有地区多边形使用 KML。
旧帖子 How can I use parse and parseKmlString together?? I need show a map loading one KML file (I use
我是一名优秀的程序员,十分优秀!