• openlayers 2 高亮显示元素以及通过属性查询高亮某一元素


    本文参考官网例子,略作修改,直接上代码:

    1、实现hover和click高亮显示

            var map, controls;
            OpenLayers.Feature.Vector.style['default']['strokeWidth'] = '2';
            var vectors;
            var highlightCtrl;
            function init(){
                map = new OpenLayers.Map('map');
    
                vectors = new OpenLayers.Layer.Vector("vector", {isBaseLayer: true});
                map.addLayers([vectors]);
    
                var feature = new OpenLayers.Feature.Vector(
                    OpenLayers.Geometry.fromWKT(
                        "POLYGON((28.828125 0.3515625, 132.1875 -13.0078125, -1.40625 -59.4140625, 28.828125 0.3515625))"
                    )
                );
                vectors.addFeatures([feature]);
    
                var feature2 = new OpenLayers.Feature.Vector(
                    OpenLayers.Geometry.fromWKT(
                        "POLYGON((-120.828125 -50.3515625, -80.1875 -80.0078125, -40.40625 -20.4140625, -120.828125 -50.3515625))"
                    )
                );
                vectors.addFeatures([feature2]);
    
                var report = function(e) {
                    OpenLayers.Console.log(e.type, e.feature.id);
                };
                
                highlightCtrl = new OpenLayers.Control.SelectFeature(vectors, {
                    hover: true,
                    highlightOnly: true,
                    renderIntent: "temporary",
                    eventListeners: {
                        beforefeaturehighlighted: report,
                        featurehighlighted: report,
                        featureunhighlighted: report
                    }
                });
    
                var selectCtrl = new OpenLayers.Control.SelectFeature(vectors,
                    {clickout: true}
                );
    
                map.addControl(highlightCtrl);
                map.addControl(selectCtrl);
    
                highlightCtrl.activate();
                selectCtrl.activate();
    
                map.setCenter(new OpenLayers.LonLat(0, 0), 1);
    
            }

    2、实现属性查询高亮某一元素,此处简单的通过button的click事件测试,代码如下:

    备注:使用hightlight和unhighlight是一样的效果

            //高亮显示
         function mySelect() { console.log(vectors.features); var feature = vectors.features[0]; highlightCtrl.select(feature); }
         //取消高亮显示
    function myUnselect() { var feature = vectors.features[0]; highlightCtrl.unselect(feature); }

    3、通过属性查询获得features

    var features = vectorlayer.getFeaturesByAttribute("name", "名称");

    将得到的features[0]传递给上面的高亮函数即可

  • 相关阅读:
    Mongodb新增的聚合方法及其Java客户端
    转载:mongoDB java驱动学习笔记
    笔记:Java反射以及动态代理
    转载:在恰当的地方使用MongoDB的WriteConcern.SAFE参数
    去Oracle事件(转)
    笔记:J2EE核心模式(待补充)
    笔记:MongoDB 基础学习
    转载:PostgreSQL SQL的性能调试 方法3-- 查 看 执 行 计 划
    转载: PostgreSQL SQL的性能调试方法2--数据库log分析
    matlab怎么设置默认路径
  • 原文地址:https://www.cnblogs.com/marost/p/6553586.html
Copyright © 2020-2023  润新知