2013-07-07 74 views
2

我在地圖中有三個kml圖層:多邊形,線條,點。 該地圖從mobile-wms-vienna示例中修改。我更改了圖層,並更改了「標籤」按鈕以更改多邊形圖層上的不透明度。 爲了確保可以看到所有功能,我需要設置z-indexing。Openlayers 2.12選擇z索引設置時的特徵

但是,我也想能夠在多邊形圖層上顯示一個彈出窗口,它被設置爲最低。我不想看到線條或點的彈出窗口。 (點可以有標籤,線條不需要標籤)。我已經閱讀了很多關於多層選擇問題的文章,但是找不到解決方案來設置z-indexing時如何選擇任何東西。

有沒有辦法做到這一點?最好按照它們添加到地圖的順序繪製圖層。 或者隨地圖和縮放變化而移動的標籤圖層?不幸的是,kml多邊形標籤固定在一個點上,因此在地圖移動或放大時可能會消失。

整個地圖代碼如下,因爲我不確定地圖中是否有其他內容影響這種行爲。

var map; 
var linetyle = new OpenLayers.Style({'strokeWidth': 2, 'strokeColor':"red",}); 

function init() { 

    document.documentElement.lang = (navigator.userLanguage || navigator.language).split("-")[0]; 

    var layerPanel = new OpenLayers.Control.Panel({ 
     displayClass: "layerPanel", 
     autoActivate: true 
    }); 

    var opButton = new OpenLayers.Control({ 
     type: OpenLayers.Control.TYPE_TOGGLE, 
     displayClass: "opButton", 
     eventListeners: { 
      activate: function() { 
       if (polygon) {polygon.setOpacity(0.4);} 
      }, 
      deactivate: function() { 
       if (polygon) {polygon.setOpacity(0.9);} 
      } 
     } 
    }); 
    layerPanel.addControls([opButton]); 

    var zoomPanel = new OpenLayers.Control.ZoomPanel(); 

    // Geolocate control for the Locate button - the locationupdated handler 
    // draws a cross at the location and a circle showing the accuracy radius. 
    var geolocate = new OpenLayers.Control.Geolocate({ 
     type: OpenLayers.Control.TYPE_TOGGLE, 
     bind: false, 
     watch: true, 
     geolocationOptions: { 
      enableHighAccuracy: false, 
      maximumAge: 0, 
      timeout: 7000 
     }, 
     eventListeners: { 
      activate: function() { 
       map.addLayer(vector); 
      }, 
      deactivate: function() { 
       map.removeLayer(vector); 
       vector.removeAllFeatures(); 
      }, 
      locationupdated: function(e) { 
       vector.removeAllFeatures(); 
       vector.addFeatures([ 
        new OpenLayers.Feature.Vector(e.point, null, { 
         graphicName: 'cross', 
         strokeColor: '#f00', 
         strokeWidth: 2, 
         fillOpacity: 0, 
         pointRadius: 10 
        }), 
        new OpenLayers.Feature.Vector(
         OpenLayers.Geometry.Polygon.createRegularPolygon(
          new OpenLayers.Geometry.Point(e.point.x, e.point.y), 
          e.position.coords.accuracy/2, 50, 0 
         ), null, { 
          fillOpacity: 0.1, 
          fillColor: '#000', 
          strokeColor: '#f00', 
          strokeOpacity: 0.6 
         } 
        ) 
       ]); 
       map.zoomToExtent(vector.getDataExtent()); 
      } 
     } 
    }); 
    zoomPanel.addControls([geolocate]); 

    map = new OpenLayers.Map({ 
     div: "map", 
     theme: null, 
     projection: new OpenLayers.Projection("EPSG:3857"), 
     displayProjection: new OpenLayers.Projection("EPSG:4326"), 
     layers: [new OpenLayers.Layer.Google("Google Satellite", {type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 22})], 
     center: new OpenLayers.LonLat(149.1, -35.3).transform('EPSG:4326', 'EPSG:3857'), 
     zoom: 10, 
     units: "m", 
     maxResolution: 38.21851413574219, 
     controls: [ 
      new OpenLayers.Control.Navigation(), 
      new OpenLayers.Control.Attribution(), 
      zoomPanel, 
      layerPanel 
     ], 

    }); 

    layerPanel.activateControl(opButton); 

    // Vector layer for the location cross and circle 
    var vector = new OpenLayers.Layer.Vector("Vector Layer"); 

    var point = new OpenLayers.Layer.Vector("points", { 
//    rendererOptions: {zIndexing: 'true'}, 
       projection: map.displayProjection, 
       strategies: [new OpenLayers.Strategy.BBOX()], 
       protocol: new OpenLayers.Protocol.HTTP({ 
        url: "kml/point.kml", 
        format: new OpenLayers.Format.KML({ 
         extractStyles: true, 
         extractAttributes: true 
        })  })  }); 
    var line = new OpenLayers.Layer.Vector("line", { 
//    rendererOptions: {zIndexing: 'true'}, 
       projection: map.displayProjection, 
       strategies: [new OpenLayers.Strategy.BBOX({resFactor: 1})], 
       styleMap: linetyle, 
       protocol: new OpenLayers.Protocol.HTTP({ 
        url: "kml/line.kml", 
        format: new OpenLayers.Format.KML({ extractStyles: false, 
         extractAttributes: true 
        })  })  }); 
    var polygon = new OpenLayers.Layer.Vector("Geology", { 
//    rendererOptions: {zIndexing: 'true'}, 
       projection: map.displayProjection, 
       strategies: [new OpenLayers.Strategy.BBOX({resFactor: 1})], 
       protocol: new OpenLayers.Protocol.HTTP({ 
       url: "kml/polygon.kml", 
        format: new OpenLayers.Format.KML({extractStyles: true,extractAttributes: true}) 
       }) }); 
      map.addLayers([point, line, polygon]); 
      polygon.setOpacity(0.5); 

//   point.setZIndex(1400); 
//   line.setZIndex(1300); 
//   polygon.setZIndex(1200); 

// Select Features/Popup 
      select = new OpenLayers.Control.SelectFeature (polygon, line, point); 

      polygon.events.on({ 
       "featureselected": onFeatureSelect, 
       "featureunselected": onFeatureUnselect 
      }), 
      line.events.on({ 
       "featureselected": onFeatureSelect, 
       "featureunselected": onFeatureUnselect 
      }), 
      point.events.on({ 
       "featureselected": onFeatureSelect, 
       "featureunselected": onFeatureUnselect 
      }), 

      map.addControl(select); 
      select.activate(); 

     function onPopupClose(evt) { 
      select.unselectAll(); 
     } 
     function onFeatureSelect(event) { 
      var feature = event.feature; 
      // Since KML is user-generated, do naive protection against 
      // Javascript. 
      var content = "<h2>"+feature.attributes.name + "</h2>" + feature.attributes.description; 
      if (content.search("<script") != -1) { 
       content = "Content contained Javascript! Escaped content below.<br>" + content.replace(/</g, "&lt;"); 
      } 
      popup = new OpenLayers.Popup.FramedCloud("chicken", 
            feature.geometry.getBounds().getCenterLonLat(), 
            new OpenLayers.Size(100,100), 
            content, 
            null, false, onPopupClose); 
      feature.popup = popup; 
      map.addPopup(popup); 
     } 
     function onFeatureUnselect(event) { 
      var feature = event.feature; 
      if(feature.popup) { 
       map.removePopup(feature.popup); 
       feature.popup.destroy(); 
       delete feature.popup; 
      } 
     }; 

}; 

的地圖可以在http://quartzspatial.net/act/map_v2.html

可以解決我的問題是here最接近的答案可以看到,但我不明白,之後在把代碼在不同的地方很多嘗試如何使用該解決方案, 我放棄。

回答

0

我剛纔一直在尋找類似的問題。您現在可能已經想出了自己的想法,但我想分享我的jsFiddle,其中我在地圖對象上使用事件偵聽器而不是選擇控件。

您不能在圖層索引中使用OpenLayers選擇控件。激活事件將始終將圖層置於頂部,並在圖層上設置z索引將禁用選擇控件。我也無法通過禁用激活事件工作中的moveontop來解決方案(在jsFiddle中)。

請看看事件偵聽器的解決方案:

var map = new OpenLayers.Map({ 
div: "map", 
projection: new OpenLayers.Projection("EPSG:3857"), 
displayProjection: new OpenLayers.Projection("EPSG:4326"), 
layers: [ 
    new OpenLayers.Layer.OSM()], 
controls: [ 
    new OpenLayers.Control.Navigation(), 
    new OpenLayers.Control.ArgParser() ], 

eventListeners: { 

    featureover: function(e) { if (e.feature.layer != vectors2) { 
    e.feature.renderIntent = "temporary"; 
    e.feature.layer.drawFeature(e.feature); } 
    }, 

featureout: function(e) { if (e.feature.layer != vectors2) { 
    e.feature.renderIntent = "default"; 
    e.feature.layer.drawFeature(e.feature); } 
}, 

featureclick: function(e) { if (e.feature.layer != vectors2) { 
    e.feature.renderIntent = "select"; 
    e.feature.layer.drawFeature(e.feature); } 
} 

} 

});