4

マップ外のリンクからポップアップを表示できるようにするための基本的な関数を作成しました。ポップアップを開く機能は正常に機能していますが、閉じることができません。

デモリンク:http ://www.catchingtherain.com/bikestats/stations.php-左側のタブ付きパネルのリンクをクリックします。

ここにもう少し詳細があります...

典型的な地図には、kmlから読み込まれたベクトルレイヤー「ステーション」に約300のフィーチャがあります。これらは、を使用してオンロードでアクティブ化されます

select = new OpenLayers.Control.SelectFeature(stations);           
stations.events.on({
                "featureselected": onFeatureSelect,
                "featureunselected": onFeatureUnselect
                });
map.addLayer(stations);
map.addControl(select);
select.activate();

これは正常に機能します-ポップアップを開いたり閉じたりできます。

マップ外のリンクを使用して、onclick = "showMyPopup([x])を呼び出しています。[x]はkmlから読み込まれたID属性です。showMyPopup関数は次のとおりです。

function showMyPopup(myID){
    for(var a = 0; a < stations.features.length; a++){    //loop through all the features
    var feature = stations.features[a];
    if (feature.attributes.ID.value == myID) {            //until it finds the one with the matching ID attribute
       var content = "<h4>" + feature.attributes.name + "</h4>" + feature.attributes.description;
       popup = new OpenLayers.Popup.FramedCloud("chicken",
                                     feature.geometry.getBounds().getCenterLonLat(),
                                     new OpenLayers.Size(200,200),
                                     content,
                                     null, true, onPopupClose);
       feature.popup = popup;
       map.addPopup(popup);
       }
    }
}

これにより、ステーションレイヤーから期待どおりに正しいポップアップが開き、マップ機能をクリックしてロードした場合と同じように、ステーションレイヤーのDOMインスペクターを使用してポップアップを表示できますが、閉じる方法がないようです。ただし、ステーションレイヤーの元の機能は正常に機能しています(開閉)。

どんな助けでも大歓迎です(多分これに取り組むより簡単な方法がありますか?)

ありがとう、ジェームズ

PSと念のため、これがonFeatureUnselect関数です...

function onFeatureUnselect(event) {
        var feature = event.feature;
        if(feature.popup) {
            map.removePopup(feature.popup);
            feature.popup.destroy();
            delete feature.popup;
        }
     }
4

2 に答える 2

2

ononPopupClose()関数は次のとおりです。

function onPopupClose(evt) {
    select.unselectAll();
}

マップから機能を選択してポップアップの閉じるアイコンをクリックすると、機能は選択解除されますが、ポップアップはまだ閉じられていません。次に、onFeatureUnselectイベントがトリガーされ、ポップアップが実際に閉じられます。

関数ごとにポップアップを作成する場合showMyPopup()、それを選択しているわけではありません。onPopupClose()が呼び出されますが、ポップアップは閉じません。onFeatureUnselectトリガーされません。

機能で機能を選択することをお勧めしますshowMyPopup()featureselectedイベントが発生し、ポップアップがによって作成されonFeatureSelect()ます。ユーザーは、ポップアップの閉じるアイコンとマップ上の選択解除機能の両方を使用してポップアップを閉じることができます。

しかし、残念ながら、コードで機能を選択し、クリックアウトで選択を解除しようとすると、OLにバグ(または予期しない動作)が発生する可能性があります。ここで説明します:http://lists.osgeo.org/pipermail/openlayers-users/2012-September/026349.html 1つの可能な修正は、SelectControl.handlers.feature.lastFeatureを手動で設定することです。

function showMyPopup(myID){
    for(var a = 0; a < stations.features.length; a++){    //loop through all the features
        var feature = stations.features[a];
        if (feature.attributes.ID.value == myID) {            //until it finds the one with the matching ID attribute

            // select is your SelectFeature control
            select.select(feature);
            // Fix for unselect bug
            select.handlers.feature.lastFeature = feature;

            break;
        }
    }
}
于 2012-11-21T08:26:53.617 に答える
0

OpenLayersのソースを見てみると、Popup.jsにはそのようなものがあります...

    ...
    var closePopup = callback || function(e) {
        this.hide();
        OpenLayers.Event.stop(e);
    };
    OpenLayers.Event.observe(this.closeDiv, "touchend", 
            OpenLayers.Function.bindAsEventListener(closePopup, this));
    OpenLayers.Event.observe(this.closeDiv, "click", 
            OpenLayers.Function.bindAsEventListener(closePopup, this));
    ...

独自のclosePopup関数を追加する場合は、コードでhide関数を呼び出す必要があるように思われます。

于 2012-11-20T21:22:02.043 に答える