2012-10-22 4 views
1

전단지 openPopup 메소드에 문제가 있습니다.전단지 및지도 상자 : OpenPopup이 작동하지 않습니다.

showMap = function(elements) { 
    var jsonp = 'http://a.tiles.mapbox.com/v3/blahblahblah.jsonp'; 
    var m = new L.Map("my_map").setView(new L.LatLng(51.5, -0.09), 15); 

    var geojsonLayer = new L.GeoJSON(); 

    var PlaceIcon = L.Icon.extend({ 
     iconSize: new L.Point(25, 41), 
     shadowSize: new L.Point(40, 35), 
     popupAnchor: new L.Point(0, -30) 
    }); 

    var icon = new PlaceIcon(__HOME__ + '/images/leaflet/marker.png'); 
    var marker; 


    for (var i = 0; i < elements.length; i++) { 
     var address = $("<div/>").html(elements[i].address).text(); 
     var latlng = new L.LatLng(elements[i].latitude, elements[i].longitude); 
     marker = new L.Marker(latlng, {icon: icon}).bindPopup(address); 

     if (i == 0) { 
      marker.openPopup(); 
     } 
     m.addLayer(geojsonLayer).addLayer(marker); 
    } 
    // Get metadata about the map from MapBox 
    wax.tilejson(jsonp, function(tilejson) { 
     m.addLayer(new wax.leaf.connector(tilejson)); 
    }); 
} 

마커를 클릭하면 팝업 창이 열립니다. 하지만지도가로드 될 때 첫 번째 팝업을 열고 싶습니다. (그리고 마커에서 다른 팝업을 클릭하십시오)

아이디어가 있습니까?

답변

2

마커를 클릭하면 팝업이 표시되지만지도가로드 될 때 자동으로 표시되는 첫 번째 마커의 팝업이 표시되지 않는다고 가정합니다.

먼저 GeoJSON을 실제로 사용하고있는 것처럼 보이지 않으므로 GeoJSON 레이어가 필요하지 않습니다 (FeatureLayer 만 사용할 수 있음)하지만 문제가 발생하지 않아야합니다. 어떤 레이어 그룹을 사용하든 한 번 맵에 추가 한 다음 모든 하위 레이어를 LayerGroup에 추가해야합니다. 현재 "for"루프에 geojsonLayer를 여러 번 추가하고 싶지 않습니다. 두 번째로 마커가지도에 추가 된 후에 marker.openPopup()에 전화해야합니다. 주위에이 같은 형태의 코드를 변경해보십시오 : 당신은지도에 마커를 추가하고 당신이 잘되어야 후 전화 openPopup 넣어

var layerGroup = new L.FeatureGroup(); 
layerGroup.addTo(m); 

for (var i = 0; i < elements.length; i++) { 
    var address = $("<div/>").html(elements[i].address).text(); 
    var latlng = new L.LatLng(elements[i].latitude, elements[i].longitude); 
    marker = new L.Marker(latlng, {icon: icon}).bindPopup(address); 

    //You don't add the marker directly to the map. The layerGroup has already 
    //been added to the map so it will take care of adding the marker to the map 
    layerGroup.addLayer(marker); 

    if (i == 0) { 
     marker.openPopup(); 
    } 
}