2013-12-16 18 views

回答

1

您可以顯示彈出窗口來顯示信息。

設置的屬性說「名」,這包含了您的功能如下信息,

var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(specify your points here), 
{some:'data'}, 
{externalGraphic: 'image for your feature', 
graphicHeight: 21, graphicWidth: 16}); 
feature.attributes = { 
name:"My feature info"}; 

創建功能添加您的載體層,並設置控制如下

Control = new OpenLayers.Control.SelectFeature(myvectorLayer, 
{ 
onSelect: onFeatureSelect, 
onUnselect: onFeatureUnselect 
}); 

後現在定義您的onFeatureSelect函數

function onFeatureSelect(feature) 
{ 
var popup = new OpenLayers.Popup.FramedCloud("MyPopUp", 
feature.geometry.getBounds().getCenterLonLat(), 
null,feature.attributes.name, null, true, onPopupClose); 
popup.panMapIfOutOfView = true; 
popup.autoSize = true; 
feature.popup = popup; 
map.addPopup(popup); 
} 

該段代碼「feature.attributes.name」會顯示你的特性的「name」屬性中存在的任何內容作爲彈出框的主體。你甚至可以在包含img標籤的html標籤組合等特性中定義你的name屬性。

+0

非常感謝。有用。我創建了2個功能,並點擊顯示地圖上的彈出窗口,但問題是當我點擊第一個功能,彈出窗口出現,當我繼續點擊第二個功能時,第一個彈出窗口仍然在地圖上,兩個彈出窗口都顯示。我想在顯示其他 – Winston

+0

之前刪除活動彈出窗口,這是因爲在創建新彈出窗口之前並未破壞活動彈出窗口。銷燬地圖上的所有活動彈出窗口,或者創建一個新窗口或在onFeatureUnselect函數中。 – DPH