2017-10-22 9 views
0

저는 데이터베이스에서 두 개의 URL을 통해 데이터를 제공하는 전단지를 가지고 있습니다. 나는 장고와 django - 전단지를 사용합니다. 파이썬 3.5, postgresql 9.6.전단지 js는 하나의 레이어를 제거하고, 다른 레이어를 추가하고 첫 번째 레이어의 변수로 필터링합니다.

페이지를로드 할 때 레이어 A가 표시됩니다. 레이어 A의 팝업을 열면 레이어 A의 항목 팝업을 클릭하여 레이어 B를로드 할 수 있습니다. LayerB의 항목은 외부 키로 연결된 LayerA의 특정 항목에 속합니다. 여기

는 jsfiddle입니다 : https://jsfiddle.net/zt56nmog/11/

을 heres 내 코드 :

형식 오류 : 나는 다음과 같은 오류 얻을

function map_init_basic (map, options) { 
    urlA = "http://127.0.0.1:8000/data.A" // Geojson 
    urlB = "http://127.0.0.1:8000/data.B" // Geojson 

    layerA = L.geoJson(null, { 
       pointToLayer: function(feature, latlng) { 
        return L.marker(latlng, {icon: hvIcon}); 
        }, 
       onEachFeature: function(feature, layer) { 
        popupText = "button to call layerA" + "<button id='theirFlats' type='button' class='btn btn-link' onclick='getLayerB(\""+feature.id+"\");'>show layerB:</button>"; 
        layer.bindPopup(popupText); 
       } 
       }); 


    layerB = L.geoJson(null, { 
       filter: function(feature, layer) { 
        {return feature.properties.id_hv == id_layerA;}; 
       }, 
       pointToLayer: function(feature, latlng) { 
        return L.marker(latlng, {icon: whgIcon}); 
       } 
       }); 


     $.getJSON(urlA, function(data){ 
       layerA.addData(data); 
     }); 

     layerA.addTo(map); 

} // end map_init 

function getLayerB(id_layerA){ 
     alert(id_layerA); // alerts the right id 
     layerA.remove(); // works, removes the layerA 

     alert(urlB) 

     $.getJSON(urlB, function(data){ 
       layerB.addData(data); 
       console.log(data); 
       }); 

     layerB.addTo(map); 
     alert(layerB) 
     map.fitBounds(layerB); 

     } 

톤가 정의되어

당신은 getLayerB를 추가 할 필요가
+0

오류가 당신이 정말로 그것을 놓친 때문에 코드를 확인하십시오 id_layerB 변수를 선언하지 않는 것이 말했다. – davojta

+0

오, 미안 해요, 네, 그건 typ-o였습니다. id_layerA 인 것을 의미합니다. 하지만 다른 오류가 거기에 무엇인지 모르겠다. – fabvys

+1

이 경우 jsfiddle을 작성할 수 있습니까? 디버그하고 아이디어를 검증하는 것이 더 쉬울 것입니다. – davojta

답변

1

팝업에 대한 onclick 처리기에 액세스 할 수있게 해주는 창으로

urlA = {"features":[{"id":1736,"type":"Feature","properties":{"hv_city_name":"Berlin","hv_id":1736},"geometry":{"type":"Point","coordinates":[13.299364,52.487194]}},{"id":2814,"type":"Feature","properties":{"hv_city_name":"Berlin","hv_id":2814},"geometry":{"type":"Point","coordinates":[13.627012,52.540632]}}]} 
 

 
urlB = {"features":[{"geometry":{"type":"Point","coordinates":[13.41001,52.49778]},"properties":{"id_hv":2814,"whg_scout_id":100176801},"id":100176801,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.6312,52.4528]},"properties":{"id_hv":2814,"whg_scout_id":100195258},"id":100195258,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.42556,52.53958]},"properties":{"id_hv":1736,"whg_scout_id":93580383},"id":93580383,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.41757,52.55054]},"properties":{"id_hv":2814,"whg_scout_id":66425039},"id":66425039,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.37035,52.53373]},"properties":{"id_hv":1736,"whg_scout_id":100201644},"id":100201644,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.24678,52.50916]},"properties":{"id_hv":2814,"whg_scout_id":100205048},"id":100205048,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.3163,52.52065]},"properties":{"id_hv":1736,"whg_scout_id":99870716},"id":99870716,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.33467,52.46926]},"properties":{"id_hv":2814,"whg_scout_id":99350260},"id":99350260,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.33144,52.48821]},"properties":{"id_hv":1736,"whg_scout_id":100031435},"id":100031435,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.2003,52.51963]},"properties":{"id_hv":2814,"whg_scout_id":100183131},"id":100183131,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.46024,52.50881]},"properties":{"id_hv":1736,"whg_scout_id":100206080},"id":100206080,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.29768,52.62473]},"properties":{"id_hv":2814,"whg_scout_id":98474448},"id":98474448,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.48831,52.51692]},"properties":{"id_hv":1736,"whg_scout_id":100183115},"id":100183115,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.47752,52.60434]},"properties":{"id_hv":2814,"whg_scout_id":100171391},"id":100171391,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.32684,52.46749]},"properties":{"id_hv":1736,"whg_scout_id":100180115},"id":100180115,"type":"Feature"},{"geometry":{"type":"Point","coordinates":[13.37858,52.50024]},"properties":{"id_hv":2814,"whg_scout_id":88565013},"id":88565013,"type":"Feature"}]} 
 

 

 

 

 
var OpenStreetMap_BlackAndWhite = L.tileLayer('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', { 
 
\t maxZoom: 18, 
 
\t attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>' 
 
}); 
 

 
var map = L.map('map') 
 
    .setView([52.5, 13.5], 10) 
 
    .addLayer(OpenStreetMap_BlackAndWhite); 
 

 
     
 
     
 
layerA = L.geoJson(urlA, { 
 
       pointToLayer: function(feature, latlng) { 
 
        return L.marker(latlng); 
 
        }, 
 
       onEachFeature: function(feature, layer) { 
 
        popupText = "button to call layerB" + "<button id='belongs2A' type='button' class='btn btn-link' onclick='getLayerB(\""+feature.id+"\");'>show layerB:</button>"; 
 
        layer.bindPopup(popupText); 
 
       }, 
 
       pointToLayer: function(feature, latlng) { 
 
        return L.circle(latlng, {color: 'red'}); 
 
       } 
 
       }); 
 

 
function getLayerB(id_layerA){ 
 
     alert(id_layerA); // alerts the right id 
 
     layerA.remove(); // works, removes the layerA 
 
     
 
     layerB = L.geoJson(urlB, { 
 
       filter: function(feature, layer) { 
 
        {return feature.properties.id_hv == parseInt(id_layerA);}; 
 
       }, 
 
       pointToLayer: function(feature, latlng) { 
 
        return L.circle(latlng, {color: 'blue'}); 
 
       } 
 
       }); 
 
     map.addLayer(layerB); 
 
     } 
 

 
// i have made reference getLayerB to window to make it accessible for onclick handler 
 
// which can call only global functions 
 
window.getLayerB = getLayerB; 
 

 

 

 
map.addLayer(layerA);
#map { 
 
    height: 500px; 
 
    width: 100%; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.2.0/leaflet.js"></script> 
 
<link href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.2.0/leaflet.css" rel="stylesheet"/> 
 
<div id="map"></div>