2016-10-20 106 views
1

我想希望把幾個標記在地圖上,這些標記是由「父」標記「孩子」標記上。所有父標記應同時在地圖上可見,而如果點擊特定父標記,則應切換開/關特定父標記的子標記我創建了以下工作示例以演示:單張:內創建單獨的事件監聽for循環

<!DOCTYPE html> 
 
<html> 
 
<head> \t 
 
\t <title>Test 1</title> 
 
\t <meta charset="utf-8" /> 
 
\t <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
\t <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" /> 
 
\t <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script> 
 
    <style> 
 
     body {padding: 0; margin: 0;} 
 
     html, body, #map {height: 100%;} 
 
    </style> 
 
</head> 
 

 
<body> 
 
<div id='map'></div> 
 

 
<script> 
 
var thunder = L.tileLayer('https://{s}.tile.thunderforest.com/neighbourhood/{z}/{x}/{y}.png', {subdomains:'abc', attribution:'<a href="http://www.thunderforest.com">Thunderforest</a> | <a href="http://www.openstreetmap.org/copyright/">OpenStreetMap</a>' }); 
 
var map = L.map('map',{layers: [thunder]}).setView([50.08, 10.08], 12); 
 
var parent1 = L.marker([50.0, 10.0]).bindPopup('Marker 1'); 
 
var parent2 = L.marker([50.1, 10.1]).bindPopup('Marker 2');; 
 
var layergroupParents = L.layerGroup([parent1, parent2]); 
 

 
layergroupParents.addTo(map); 
 

 
var child1a = L.marker([50.02, 10.02],{title: 'Child 1a'}); 
 
var child1b = L.marker([50.04, 10.04],{title: 'Child 1b'}); 
 
var layergroupChilds1 = L.layerGroup([child1a, child1b]); 
 
var child2a = L.marker([50.12, 10.12],{title: 'Child 2a'}); 
 
var child2b = L.marker([50.14, 10.14],{title: 'Child 2b'}); 
 
var layergroupChilds2 = L.layerGroup([child2a, child2b]); 
 

 
parent1.on('click', function(){ 
 
    if (map.hasLayer(layergroupChilds1)) { 
 
\t  map.removeLayer(layergroupChilds1) 
 
    } else { 
 
     layergroupChilds1.addTo(map); 
 
\t } 
 
}); 
 

 
parent2.on('click', function(){ 
 
    if (map.hasLayer(layergroupChilds2)) { 
 
\t  map.removeLayer(layergroupChilds2) 
 
    } else { 
 
     layergroupChilds2.addTo(map); 
 
\t } 
 
}); 
 

 
</script> 
 
</body> 
 
</html>

因爲我想顯示相當多的標誌,我想for循環的幫助下把標記的特性爲Array(「markerArray」),並處理其標誌:

<!DOCTYPE html> 
 
<html> 
 
<head> \t 
 
\t <title>Test 2</title> 
 
\t <meta charset="utf-8" /> 
 
\t <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 
\t <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" /> 
 
\t <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script> 
 
    <style> 
 
     body {padding: 0; margin: 0;} 
 
     html, body, #map {height: 100%;} 
 
    </style> 
 
</head> 
 

 
<body> 
 
<div id='map'></div> 
 

 
<script> 
 
var thunder = L.tileLayer('https://{s}.tile.thunderforest.com/neighbourhood/{z}/{x}/{y}.png', {subdomains:'abc', attribution:'<a href="http://www.thunderforest.com">Thunderforest</a> | <a href="http://www.openstreetmap.org/copyright/">OpenStreetMap</a>' }); 
 
var map = L.map('map',{layers: [thunder]}).setView([50.08, 10.08], 12); 
 

 
var markerArray = [ 
 
    ["Marker 1", 50.0, 10.0, [["Child 1a", 50.02, 10.02], ["Child 1b", 50.04, 10.04]]], 
 
    ["Marker 2", 50.1, 10.1, [["Child 1a", 50.12, 10.12], ["Child 1b", 50.14, 10.14]]], 
 
]; 
 

 
var numberParents = markerArray.length; 
 
var layergroupParents = L.layerGroup(); 
 
for (cnt1=0; cnt1<numberParents; cnt1++) { 
 
    var parentObject = L.marker([markerArray[cnt1][1], markerArray[cnt1][2]]); 
 
    parentObject.bindPopup(markerArray[cnt1][0]); 
 
    layergroupParents.addLayer (parentObject); 
 

 
    var numberChilds = markerArray[cnt1][3].length;   
 
    var layergroupChilds = L.layerGroup(); 
 

 
    for (cnt2=0; cnt2<numberChilds; cnt2++) { 
 
     var childObject = L.marker([markerArray[cnt1][3][cnt2][1], markerArray[cnt1][3][cnt2][2]], {title: markerArray[cnt1][3][cnt2][0]}); 
 
     layergroupChilds.addLayer (childObject); 
 
    } 
 

 
    parentObject.on('click', function(){ 
 
     if (map.hasLayer(layergroupChilds)) { 
 
\t \t  map.removeLayer(layergroupChilds) 
 
\t \t } else { 
 
      layergroupChilds.addTo(map); 
 
\t \t } 
 
    }); 
 

 

 
} 
 
layergroupParents.addTo(map); 
 

 
</script> 
 
</body> 
 
</html>

如果執行這些腳本,你會看到,有一些問題,每一個人家長標誌分配一個單獨的「click」事件。現在,即使我點擊第一個父標記,最後父標記的子標記也會打開/關閉。

你知道我該如何解決這些問題,腳本2是否像腳本1一樣工作?

回答

0

對於它的工作,你必須給孩子附加到父(使用JavaScript屬性)

var layergroupChilds = L.layerGroup(); 
parentObject.layergroupChilds = layergroupChilds; 

當父母收到一個點擊,你可以找到在事件e父對象,因此,它的孩子。

parentObject.on('click', function(e){ 
    if (map.hasLayer(e.target.layergroupChilds)) { 
     map.removeLayer(e.target.layergroupChilds) 
    } else { 
     e.target.layergroupChilds.addTo(map); 
    } 
}); 

這是您code與修正