openlayers2渐变色渲染

概述

在前文中,讲到了oL2中唯一值渲染的实现方式,在本文讲述ol2中渐变色渲染的实现方式。

效果

代码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>openlayers map</title>
    <link rel="stylesheet" href="../../../plugin/OpenLayers-2.13.1/theme/default/style.css" type="text/css">
    <style>
        html, body, #map{
            padding:0;
            margin:0;
            height:100%;
            width:100%;
            overflow: hidden;
        }
    </style>
    <script src="../../../plugin/OpenLayers-2.13.1/OpenLayers.js"></script>
    <script src="../../../plugin/jquery/jquery-1.8.3.js"></script>
    <script>
        var map;
        var tiled;
        OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
        OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;
        $(window).load(function() {
            var format = 'image/png';
            var bounds = new OpenLayers.Bounds(
                    73.45100463562233, 18.16324718764174,
                    134.97679764650596, 53.531943152223576
            );
            var options = {
                controls: [],
                maxExtent: bounds,
                maxResolution: 0.2403351289487642,
                projection: "EPSG:4326",
                units: 'degrees'
            };
            map = new OpenLayers.Map('map', options);
            tiled = new OpenLayers.Layer.WMS(
                    "Geoserver layers - Tiled",
                    "http://localhost:8088/geoserver/lzugis/wms",
                    {
                        "LAYERS": 'province',
                        "STYLES": '',
                        format: format
                    },
                    {
                        buffer: 0,
                        displayOutsideMaxExtent: true,
                        isBaseLayer: true,
                        yx : {'EPSG:4326' : true}
                    }
            );
            map.addLayers([tiled]);
            OpenLayers.INCHES_PER_UNIT["千米"] = OpenLayers.INCHES_PER_UNIT["km"];
            OpenLayers.INCHES_PER_UNIT["米"] = OpenLayers.INCHES_PER_UNIT["m"];
            OpenLayers.INCHES_PER_UNIT["英里"] = OpenLayers.INCHES_PER_UNIT["mi"];
            OpenLayers.INCHES_PER_UNIT["英寸"] = OpenLayers.INCHES_PER_UNIT["ft"];
            //比例尺
            map.addControl(new OpenLayers.Control.ScaleLine({topOutUnits:"千米",topInUnits:"米",bottomOutUnits:"英里",
                bottomInUnits:"英寸"
            }));
            map.addControl(new OpenLayers.Control.Zoom());
            map.addControl(new OpenLayers.Control.Navigation());
            map.addControl(new OpenLayers.Control.OverviewMap());
            map.zoomToExtent(bounds);
            var style = new OpenLayers.Style({
                strokeColor: "#ffffff",
                strokeWidth: 1,
//                label: "${vocation}",
                labelAlign: "center",
                labelXOffset: "0",
                labelYOffset: "-0",
                fontColor: "#000000",
                fontFamily: "微软雅黑",
                fontSize:13
            },{
                rules: [
                    new OpenLayers.Rule({
                        // a rule contains an optional filter
                        filter: new OpenLayers.Filter.Comparison({
                            type: OpenLayers.Filter.Comparison.LESS_THAN,
                            property: "vocation", // the "foo" feature attribute
                            value: 2
                        }),
                        // if a feature matches the above filter, use this symbolizer
                        symbolizer: {
                            fillColor: "#2892c7"
                        }
                    }),
                    new OpenLayers.Rule({
                        filter: new OpenLayers.Filter.Comparison({
                            type: OpenLayers.Filter.Comparison.BETWEEN,
                            property: "vocation",
                            lowerBoundary: 2,
                            upperBoundary: 6
                        }),
                        symbolizer: {
                            fillColor: "#a0c29b"
                        }
                    }),
                    new OpenLayers.Rule({
                        filter: new OpenLayers.Filter.Comparison({
                            type: OpenLayers.Filter.Comparison.BETWEEN,
                            property: "vocation",
                            lowerBoundary: 6,
                            upperBoundary: 9
                        }),
                        symbolizer: {
                            fillColor: "#fafa64"
                        }
                    }),
                    new OpenLayers.Rule({
                        filter: new OpenLayers.Filter.Comparison({
                            type: OpenLayers.Filter.Comparison.BETWEEN,
                            property: "vocation",
                            lowerBoundary: 9,
                            upperBoundary: 14
                        }),
                        symbolizer: {
                            fillColor: "#fa8d34"
                        }
                    }),
                    new OpenLayers.Rule({
                        // apply this rule if no others apply
                        elseFilter: true,
                        symbolizer: {
                            fillColor: "#e81014"
                        }
                    })
                ]
            });
            var vector_layer = new OpenLayers.Layer.Vector("pro",{
                styleMap: new OpenLayers.StyleMap(style)
            });
            map.addLayer(vector_layer);
            $("#geojson").on("click",function(){
                $.get("data/province.geojson",null,function(result){
                    result = eval("("+result+")");
                    var geojson_format = new OpenLayers.Format.GeoJSON();
                    vector_layer.addFeatures(geojson_format.read(result));
                });
            });
        });
    </script>
</head>
<body>
<div id="map">
    <div style="position: absolute;top: 10pt;right: 10pt;z-index: 999;background: #fff;border: 1px solid #f00;padding: 10px;">
        <button id="geojson">add GeoJSON</button>
    </div>
</div>
</body>
</html>

传播GIS知识 | 交流GIS经验 | 分享GIS价值 | 专注GIS发展

技术博客

http://blog.csdn.net/gisshixisheng

在线教程

http://edu.csdn.net/course/detail/799
Github

https://github.com/lzugis/

联系方式

q       q:1004740957

e-mail:niujp08@qq.com

公众号:lzugis15

Q Q 群:452117357(webgis)
             337469080(Android)

转载自:https://blog.csdn.net/GISShiXiSheng/article/details/51828417

You may also like...