2011-10-09 91 views
3

我使用這段代碼在地圖上加載kml文件,在這種情況下我使用OpenStreetMap。
我想知道如何在點擊kml(道路)時添加一個彈出窗口,以便顯示有關它的一些信息。
在openlayers中添加彈出信息?

var line_1 = new OpenLayers.Layer.GML(
    'Line - 1', 
    "lines/line_1.kml", 
    { 
     visibility: true, 
     format: OpenLayers.Format.KML, 
     style: 
     { 
      strokeWidth: 4, 
      strokeColor: "#ff0000", 
      strokeOpacity: 1 
     }, 
     projection: map.displayProjection 
    } 
); 
+0

我想做類似以下的操作:http://openlayers.org/dev/examples/dynamic-text-layer.html和http://openlayers.org/dev/examples/select-feature-openpopup.html但我只是不知道如何把它們放在一起...:/ –

+0

你有沒有試過我的建議?任何意見? –

回答

4

GML層實際上是一個使用GML數據實例化的矢量層。所以你可以看看如何使用矢量圖層來打開彈出窗口。你的例子已經這樣做了。

他們使用一個選擇控制,並且在選擇功能打開彈出:

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

polygonLayer你的情況是line_1代替。

創建方法onFeatureSelect,你打開一個彈出:

 function onFeatureSelect(feature) { 
     selectedFeature = feature; 
     popup = new OpenLayers.Popup.FramedCloud("chicken", 
           feature.geometry.getBounds().getCenterLonLat(), 
           null, 
           "<div style='font-size:.8em'>Feature: " + feature.id +"<br>Area: " + feature.geometry.getArea()+"</div>", 
           null, true, onPopupClose); 
     feature.popup = popup; 
     map.addPopup(popup); 
    } 

哪裏map是你的地圖對象。

試一下,評論你的進度或問題。

+0

請指導我這個問題 - http://stackoverflow.com/questions/12470470/show-pop-up-box-on-google-map-sometime-it-didnt-show-it – Piraba