2011-09-17 6 views
7

私のプログラムの以前のバージョンでは、markersを使って地図上のポイントに印を付けました。現在のバージョンでは、私はmarkersからvectorsに変更する必要があったため、余分な柔軟性が必要でした。OpenLayersのベクターにポップアップボックスを追加するには?

function createPopupBoxFeature(vector, lonLat, description) { 
    var feature = new OpenLayers.Feature(vector, lonLat); 

    feature.closeBox = true; 
    feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, 
     { "autoSize": true }); 
    feature.data.popupContentHTML = description; 

    vector.events.register("mousedown", feature, function(evt) { 
     if (this.popup == null) { 
      this.popup = this.createPopup(this.closeBox); 
      map.addPopup(this.popup); 
      this.popup.show(); 
     } else { 
      this.popup.toggle(); 
     } 
     OpenLayers.Event.stop(evt); 
    }); 

    return feature; 
} 

をしかし、彼らは何のevents性質を持っていないので、それはもはや、vectorsのために働いている:マーカー・ソリューションでは、私は、マーカーにポップアップボックスを追加するには、以下の機能を使用しません。これをどうやって解決するのですか?

答えて

7

自分で解決しました。

// Used to display the dialog popup 
var selectControl; 
var selectedFeature; 

selectControl = new OpenLayers.Control.SelectFeature(vectorLayer, 
    { 
     onSelect: onFeatureSelect, 
     onUnselect: onFeatureUnselect 
    }); 
    map.addControl(selectControl); 
    selectControl.activate(); 

イベントハンドラ

​​

ストアベクトルの名前でポップアップの内容SelectFeatureを追加:ここではどのようにあります。よりよい解決策があるかもしれませんが、私は気にしません。ベクトルにポップアップを追加することは、すでに難しいです。

vector.name = "Your popup content"; 
12

は、実際にそれを行うための公式の方法は以下の通りです:

(注:変数のいくつかは、これらの断片で宣言されていない:longt、緯度、マップ)

http://dev.openlayers.org/examples/light-basic.html

//Step 1 - create the vector layer 
var vectorLayer = new OpenLayers.Layer.Vector("ExampleLayer",{ 
    eventListeners:{ 
     'featureselected':function(evt){ 
      var feature = evt.feature; 
      var popup = new OpenLayers.Popup.FramedCloud("popup", 
       OpenLayers.LonLat.fromString(feature.geometry.toShortString()), 
       null, 
       feature.attributes.message+"<br>"+feature.attributes.location, 
       null, 
       true, 
       null 
      ); 
      popup.autoSize = true; 
      popup.maxSize = new OpenLayers.Size(400,800); 
      popup.fixedRelativePosition = true; 
      feature.popup = popup; 
      map.addPopup(popup); 
     }, 
     'featureunselected':function(evt){ 
      var feature = evt.feature; 
      map.removePopup(feature.popup); 
      feature.popup.destroy(); 
      feature.popup = null; 
     } 
    } 
}); 

//Step 2 - add feature to layer 
var p = new OpenLayers.Geometry.Point(longt, lat); 
var feature = new OpenLayers.Feature.Vector(
    p.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()), 
    {message:'foo', location:'bar'}, 
    {externalGraphic: '../img/marker.png', graphicHeight: 21, graphicWidth: 16} 
); 
vectorLayer.addFeatures(feature); 

//Step 3 - create the selectFeature control 
var selector = new OpenLayers.Control.SelectFeature(vectorLayer,{ 
    hover:true, 
    autoActivate:true 
}); 

//Step 4 - add the layer and control to the map 
map.addControl(selector); 
map.addLayer(vectorLayer);