2011-03-31 26 views
3

我有功能ID,我可以抓住GeoRSS流派上的標記圖層,但我仍然不確定如何導致彈出窗口以編程方式顯示。在OpenLayers(OSM)中觸發一個功能的彈出窗口的正確方法是什麼?

如果需要的話,我會根據需要創建彈出窗口,但好像我應該能夠獲取地圖上繪製的標記的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

當一個彈出添加到彈出的圖層,其自動打開。你可以發佈你的開發的一些源代碼嗎? – 2011-03-31 12:53:56

+0

@Fran〜我正在添加一個georss圖層,我只是想能夠製作一些類似google的地圖頁面,點擊左側的鏈接會使彈出窗口顯示在右側。我真的不認爲我的任何代碼都意味着什麼。這是在字面上建立東西在左側顯示,然後對於地圖它是'map = new OpenLayers.Map('myMap'); map.addLayer(new OpenLayers.Layer.OSM()); map.addLayer(新的OpenLayers.Layer.GeoRSS(name,url));'我寫的任何其他代碼都不會涉及到手頭的問題,我敢肯定。 – jcolebrand 2011-03-31 14:52:14

+0

你是否有足夠的能力來解決這個問題? – 2011-04-18 13:56:43

回答

4

您不必單擊該功能即可打開一個彈出窗口。

首先,您需要從功能ID引用功能。我會在GeoRSS層的loadend事件中使用圖層上的markers屬性來完成此操作。

假設你有你的功能一個參考,我會寫它處理自動彈出一個方法:

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

「因爲我需要從」loadend事件「中獲取信息的引用」 - 有哪些信息?特徵特定信息存儲在feature.attributes中,如果需要圖層信息,則需要使用feature.layer屬性來獲取它。你需要的唯一信息就是這個功能的ID,而且我理解你有這個功能嗎? – 2011-04-04 15:57:33

+0

犯錯,劃傷所有,是的是的,我有HTML內容已經/ facepalm ...再次感謝 – jcolebrand 2011-04-04 16:05:27

+1

沒有需要facepalms :-) – 2011-04-04 16:16:43

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作爲一個全球性的,所以我不必每次重新獲取它,我想用它

相關問題