2011-03-31 14 views
3

필자는 기능 ID를 가지고 있기 때문에 GeoRSS로드 엔드에서 마커 레이어를 잡을 수는 있지만 프로그래밍 방식으로 팝업을 표시하는 방법을 아직 모르겠습니다.OSM (OpenLayers)에서 기능에 대한 팝업을 트리거하는 적절한 방법은 무엇입니까?

필요에 따라 팝업을 만들 겠지만지도에 그려진 마커의 ID를 가져와 해당 이벤트를 호출 할 수 있어야합니다. jQuery를 사용하여 시도하고지도 요소에 $(marker-id).click() 이벤트를 호출하지만, 작동하지 않는 것 같습니다. 내가 뭘 놓치고 있니? 지금까지 나는 곳

내가 코드에 대한 질문을 받았다, 나는 추정 이후는 보일러 수 있기 때문에

은 여기 :

map = new OpenLayers.Map('myMap'); 
map.addLayer(new OpenLayers.Layer.OSM()); 
map.addLayer(new OpenLayers.Layer.GeoRSS(name,url)); 

//I've done some stuff as well in re: projections and centering and 
//setting extents, but those really don't pertain to this question. 
다른 곳

내가 나에게 좋은 목록의 jQuery 템플릿의 비트를 수행하고 구축했습니다 지도에 표시되는 모든 점을 표시합니다. 나는 레이어 loadend에서 콜백을 수행하고 레이어 객체를 얻는 방법을 알고, 수동으로 맵에서 레이어를 검색하는 방법을 알고, 레이어 컬렉션을 반복하고 레이어를 찾는 방법을 알고있다. 그래서 팝업에 대한 세부 정보를 얻을 수는 있지만 DOM 또는이 API의 기본 제공 메서드를 사용하여 element.click()을 쉽게 만들 수있는 방법을 알지 못합니다. 이는 내가 선호하는 것입니다. 해야 할 것.

+0

당신이 팝업의 층의 자동 개방에 팝업을 추가하면 사용할 때마다 그것을 재 취득 할 필요가 없습니다. 당신의 개발의 작은 소스 코드를 게시 할 수 있습니까? –

+0

@Fran ~ georss 레이어를 추가하고 있으며 Google지도 페이지와 같은 것을 만들 수 있기를 원합니다. 왼쪽의 링크를 클릭하면 오른쪽에 팝업이 표시됩니다. 나는 정말로 내 코드가 지금까지 아무 것도 의미하지 않는다고 생각한다. 그것은 문자 그대로 표시를 위해 왼쪽에있는 것을 구성하고지도에 대해서는 map = new OpenLayers.Map ('myMap'); map.addLayer (새로운 OpenLayers.Layer.OSM()); map.addLayer (new OpenLayers.Layer.GeoRSS (name, url));'내가 작성한 다른 코드는 당면한 질문과 관련이 없습니다. – jcolebrand

+0

문제를 해결하기에 충분합니까? –

답변

4

팝업을 열려면이 기능을 클릭 할 필요가 없습니다.

먼저 기능 ID에서 기능에 대한 참조가 필요합니다. 레이어의 markers 속성을 사용하여 GeoRSS 레이어의 loadend 이벤트에서이 작업을 수행합니다.

당신이 당신의 기능에 대한 참조를 가지고 가정, 나는 자동 팝업 처리하는 방법을 작성합니다

var popups = {}; // to be able to handle them later 

function addPopup(feature) { 

var text = getHtmlContent(feature); // handle the content in a separate function. 

var popupId = evt.xy.x + "," + evt.xy.y; 
var popup = popups[popupId]; 
if (!popup || !popup.map) { 
    popup = new OpenLayers.Popup.Anchored(
     popupId, 
     feature.lonlat, 
     null, 
     " ", 
     null, 
     true, 
     function(evt) { 
      delete popups[this.id]; 
      this.hide(); 
      OpenLayers.Event.stop(evt); 
     } 
    ); 
    popup.autoSize = true; 
    popup.useInlineStyles = false; 
    popups[popupId] = popup; 
    feature.layer.map.addPopup(popup, true); 
} 
popup.setContentHTML(popup.contentHTML + text); 
popup.show(); 

} 
+0

"로드 엔드 이벤트의 정보에 대한 참조를 얻으려고"- 그 정보는 무엇입니까? 기능 관련 정보는 feature.attributes에 저장되며, 레이어 정보가 필요한 경우 가져 오기위한 feature.layer 속성이 있습니다. 로드 엔드 이벤트에서 필요한 유일한 정보는 해당 기능의 ID이며, 귀하가 가지고있는 그대로 이해합니다. –

+0

실수, 스크래치 모두 그래, 그래, 난 HTML 콘텐츠가 이미/facepalm ... 감사합니다 – jcolebrand

+1

facepalms에 대한 필요가 없습니다 :-) –

1

FWIW 나는 마침내 이것에 와서 완전히 다른 무언가를했다,하지만 그의 대답은 좋았다 하나.

//I have a list of boxes that contain the information on the map (think google maps) 
$('.paginatedItem').live('mouseenter', onFeatureSelected).live('mouseleave',onFeatureUnselected); 

function onFeatureSelected(event) { 
    // I stuff the lookup attribute (I'm lazy) into a global 
    // a global, because there can be only one 
    hoveredItem = $(this).attr('lookup'); 

    /* Do something here to indicate the onhover */ 
    // find the layer pagination id 
    var feature = findFeatureById(hoveredItem); 

    if (feature) { 

     // use the pagination id to find the event, and then trigger the click for that event to show the popup 
     // also, pass a null event, since we don't necessarily have one. 
     feature.marker.events.listeners.click[0].func.call(feature, event) 
    } 
} 
function onFeatureUnselected(event) { 
    /* Do something here to indicate the onhover */ 
    // find the layer pagination id 
    var feature = findFeatureById(hoveredItem); 

    if (feature) { 

     // use the pagination id to find the event, and then trigger the click for that event to show the popup 
     // also, pass a null event, since we don't necessarily have one. 
     feature.marker.events.listeners.click[0].func.call(feature, event) 
    } 

    /* Do something here to stop the indication of the onhover */ 

    hoveredItem = null; 
} 

function findFeatureById(featureId) { 
    for (var key in map.layers) { 
     var layer = map.layers[key]; 
     if (layer.hasOwnProperty('features')) { 
      for (var key1 in layer.features) { 
       var feature = layer.features[key1]; 
       if (feature.hasOwnProperty('id') && feature.id == featureId) { 
        return feature; 
       } 
      } 
     } 
    } 
    return null; 
} 

또한 내가 세계로 map을 유지 있습니다 그래서 나는