当前位置: 代码迷 >> JavaScript >> 如何在地理地图上显示/隐藏?
  详细解决方案

如何在地理地图上显示/隐藏?

热度:94   发布时间:2023-06-05 11:58:52.0

到目前为止,我终于完成了一个地理地图,其中包含带有连接它们的线的节点。 所有节点都有所有者ID和母公司。 但是我想要一个可以双击节点的函数,它只会显示所选的节点以及它所连接的节点。 我不知道如何执行此操作,因为我不知道如何将我创建的线连接到节点。 我在JSFiddle上重新创建了示例: : 。 随时提出任何问题,并预先感谢您!

ownerID是公司,parentCompany是拥有所选节点的节点:

name: 'Tesco',
radius: 6,
fillKey: 'Negative',
holderID: 2,
parentCompany: 1,
balance: '$132393',
date: '11/07/2005',
latitude: 48.632909,
longitude: 35.024414

您可以选择nodeslinks widh D3.js:

var bnodes = d3.selectAll('.datamaps-bubble');
var blinks = d3.selectAll('.datamaps-arc');

然后,您可以计算其他信息以通过双击更有效地执行节点和链接的选择,例如:

blinks.each(function(dl, il) {
    bnodes.each(function(d,i) {
        if (dl.origin.latitude == d.latitude) {
            dl.origin.holderID = d.holderID;
        } else if (dl.destination.longitude == d.longitude) {
            dl.destination.holderID = d.holderID;
        }
    })
});

然后在双击event上仅显示链接的节点和链接:

bnodes.on('dblclick', function(d, i) {
    blinks.classed('hide', true);
    bnodes.classed('hide', true);

    connectedNodes = [d.holderID];

    blinks.filter(function(dl,il) {
        if (dl.origin.holderID == d.holderID) {
            connectedNodes.push(dl.destination.holderID);
            return true;
        } else if (dl.destination.holderID == d.holderID) {
            connectedNodes.push(dl.origin.holderID);
            return true;
        }
        return false;
    }).classed('hide', false);

    bnodes.filter(function(dd,ii) {
        return connectedNodes.indexOf(dd.holderID) > -1;
    }).classed('hide', false);
});

最后是小提琴: : 和代码段:

 //creating the map var map = new Datamap({ element: document.getElementById('map'), scope: 'world', geographyConfig: { popupOnHover: false, highlightOnHover: false }, fills: { //node colour 'Positive': '#007D1C', 'Negative': '#A51000', //map colour defaultFill: '#9AAEBF', }, arcConfig: { strokeColor: 'black', strokeWidth: 10, arcSharpness: 1, animationSpeed: 600 }, }); var nodes = [{ name: 'LRZ', radius: 6, fillKey: 'Positive', holderID: 1, parentCompany: 0, balance: '$10032393', date: '17/07/2015', latitude: 51.727028, longitude: -0.395508 }, { name: 'Tesco', radius: 6, fillKey: 'Negative', holderID: 2, parentCompany: 1, balance: '$132393', date: '11/07/2005', latitude: 48.632909, longitude: 35.024414 }, { name: 'Apple', radius: 6, fillKey: 'Positive', holderID: 3, parentCompany: 1, balance: '$100393', date: '01/03/2045', latitude: 37.244200, longitude: -115.815167 }, { name: 'BP', radius: 6, fillKey: 'Negative', holderID: 4, parentCompany: 1, balance: '$10032393', date: '17/07/2015', latitude: 62.955223, longitude: 109.555664 }, { name: 'Polycom', radius: 6, fillKey: 'Negative', holderID: 5, parentCompany: 2, balance: '$10032393', date: '17/07/2015', latitude: 62.955223, longitude: 109.555664 }, { name: 'Nike', radius: 6, fillKey: 'Negative', holderID: 6, parentCompany: 0, balance: '$10032393', date: '17/07/2015', latitude: -25.799891, longitude: 142.207031 }, { name: 'HTC', radius: 6, fillKey: 'Positive', holderID: 7, parentCompany: 3, balance: '$10032393', date: '17/07/2015', latitude: -3.513421, longitude: 24.082031 }, { name: 'Microsoft', radius: 6, fillKey: 'Positive', holderID: 8, parentCompany: 4, balance: '$10032393', date: '17/07/2015', latitude: -12.554564, longitude: -43.769531 }, { name: 'HP', radius: 6, fillKey: 'Negative', holderID: 9, parentCompany: 4, balance: '$10032393', date: '17/07/2015', latitude: 52.696361, longitude: -93.339844 }, { name: 'Poundland', radius: 6, fillKey: 'Negative', holderID: 10, parentCompany: 4, balance: '$10032393', date: '17/07/2015', latitude: 7.710992, longitude: -5.097656 }, { name: 'BBC', radius: 6, fillKey: 'Positive', holderID: 11, parentCompany: 6, balance: '$10032393', date: '17/07/2015', latitude: 71.187754, longitude: -36.035156 }, { name: 'Sony', radius: 6, fillKey: 'Negative', holderID: 12, parentCompany: 6, balance: '$10032393', date: '17/07/2015', latitude: 22.268764, longitude: 78.574219 }, { name: 'LG', radius: 6, fillKey: 'Positive', holderID: 13, parentCompany: 9, balance: '$10032393', date: '17/07/2015', latitude: 65.072130, longitude: 14.238281 } ]; var arcs = [ //red lines { origin: { latitude: 48.632909, longitude: 35.024414 }, destination: { latitude: 51.727028, longitude: -0.395508 }, }, { origin: { latitude: 37.244200, longitude: -115.815167 }, destination: { latitude: 51.727028, longitude: -0.395508 }, }, { origin: { latitude: 62.955223, longitude: 109.555664 }, destination: { latitude: 51.727028, longitude: -0.395508 }, }, { origin: { latitude: 48.632909, longitude: 35.024414 }, destination: { latitude: 62.955223, longitude: 109.555664 }, }, { origin: { latitude: 37.244200, longitude: -115.815167 }, destination: { latitude: -3.513421, longitude: 24.082031 }, }, { origin: { latitude: -12.554564, longitude: -43.769531 }, destination: { latitude: 62.955223, longitude: 109.555664 }, }, { origin: { latitude: 52.696361, longitude: -93.339844 }, destination: { latitude: 62.955223, longitude: 109.555664 }, }, { origin: { latitude: 7.710992, longitude: -5.097656 }, destination: { latitude: 62.955223, longitude: 109.555664 }, }, { origin: { latitude: 71.187754, longitude: -36.035156 }, destination: { latitude: -25.799891, longitude: 142.207031 }, }, { origin: { latitude: 22.268764, longitude: 78.574219 }, destination: { latitude: -25.799891, longitude: 142.207031 }, }, { origin: { latitude: 65.072130, longitude: 14.238281 }, destination: { latitude: 52.696361, longitude: -93.339844 }, }, ]; map.arc(arcs, {strokeWidth: 1, arcSharpness: 1.4}); //draw bubbles for nodes map.bubbles(nodes, { popupTemplate: function(geo, data) { return ['<div class="hoverinfo">' + 'Name: ' + data.name, '<br/>HolderID: ' + data.holderID, '<br/>Balance: ' + data.balance + '', '<br/>Date: ' + data.date + '', '</div>' ].join(''); } }); var bnodes = d3.selectAll('.datamaps-bubble'); var blinks = d3.selectAll('.datamaps-arc'); blinks.each(function(dl, il) { bnodes.each(function(d,i) { if (dl.origin.latitude == d.latitude) { dl.origin.holderID = d.holderID; } else if (dl.destination.longitude == d.longitude) { dl.destination.holderID = d.holderID; } }) }); bnodes.on('dblclick', function(d, i) { blinks.classed('hide', true); bnodes.classed('hide', true); connectedNodes = [d.holderID]; blinks.filter(function(dl,il) { if (dl.origin.holderID == d.holderID) { connectedNodes.push(dl.destination.holderID); return true; } else if (dl.destination.holderID == d.holderID) { connectedNodes.push(dl.origin.holderID); return true; } return false; }).classed('hide', false); bnodes.filter(function(dd,ii) { return connectedNodes.indexOf(dd.holderID) > -1; }).classed('hide', false); }); 
 body { padding: 0px; margin: 0px; background-color: #C0D8ED; } div#map { position: relative; width: 99%; height: 200px; margin: auto; margin-top: -25%; } h1 { color: #193369; font-family: arial; font-size: 30px; text-align: center; } .hide { display: none; } 
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Intercompany Map</title> <link rel="stylesheet" href="css/main.css" type="text/css" /> <script src="http://d3js.org/d3.v3.min.js"></script> <script src="http://d3js.org/topojson.v1.min.js"></script> <script src="http://datamaps.github.io/scripts/datamaps.world.min.js"></script> </head> <body> <h1>My D3 Node Map</h1> <div id="map" style="height:800px"></div> <script src="js/icm.js"></script> </body> </html> 

  相关解决方案