在一个displayLevel中显示featureLayer中的数据

时间:2014-07-10 16:03:19

标签: javascript map dojo arcgis-js-api

我在两个树的displayLevels中显示Basemap上的信息时遇到问题。 我有一个受这个esri示例启发的程序:https://developers.arcgis.com/javascript/jssamples/data_gas_prices.html

在此之后我构建了自己的zoomLevels或显示Levels,因为我只需要三个。 我的问题是我想在我的featureLayer中只有三个zoomLevels文本中的两个,我不知道如何做到这一点。我在互联网上找到的唯一示例是此示例表格Esri:https://developers.arcgis.com/javascript/jssamples/map_switch_layer_on_zoom.html

但他们使用ArcGISTiledMapServiceLayer而我不是。

那么请有人告诉我这个问题的解决方案吗?

以下是代码:

define([
"dojo/_base/declare",
"dojo/_base/array",
"esri/map", 
"esri/graphic",
"esri/lang",
"esri/geometry/Extent",
"esri/symbols/SimpleLineSymbol",
"esri/symbols/SimpleFillSymbol",
"esri/renderers/SimpleRenderer",
"esri/renderers/ClassBreaksRenderer",
"esri/symbols/Font",
"esri/symbols/TextSymbol",
"esri/layers/FeatureLayer", 
"esri/dijit/Legend", 
"esri/dijit/OverviewMap",
"esri/InfoTemplate",
"esri/request",

"modules/mapTip",

"dijit/TooltipDialog", 
"dijit/popup",

"dojo/json", 
"dojo/store/JsonRest",
"dojo/_base/array", 
"dojo/parser",
"dojo/_base/connect", 
"dojo/_base/Color", 
"dojo/number", 
"dojo/dom",
"dojo/dom-construct",
"dojo/dom-style",     

"dijit/form/Button",
"dijit/layout/BorderContainer", 
"dijit/layout/ContentPane", 
"dijit/layout/AccordionContainer",    

"dojo/domReady!"
], function(
    declare,array, Map, Graphic, esriLang, Extent, SimpleLineSymbol, SimpleFillSymbol, SimpleRenderer, 
    ClassBreaksRenderer, Font, TextSymbol,FeatureLayer, Legend, OverviewMap, InfoTemplate, esriRequest,Tip, 
    TooltipDialog, dijitPopup,JSON, JsonRest, arr, parser, conn, Color, number, dom, domConstruct, domStyle, Button) {
        return declare (null, {
            init: function (){
                var lods = [
                    //{"level" : 1, "resolution" : 78271.5169639999, "scale" : 295828763.795777}, 
                    //{"level" : 2, "resolution" : 39135.7584820001, "scale" : 147914381.897889}, 
                    //{"level" : 3, "resolution" : 19567.8792409999, "scale" : 73957190.948944}, 
                    //{"level" : 4, "resolution" : 9783.93962049996, "scale" : 36978595.474472}, 
                    //{"level" : 5, "resolution" : 4891.96981024998, "scale" : 18489297.737236}, 
                    //{"level" : 6, "resolution" : 2445.98490512499, "scale" : 9244648.868618}, 
                    {"level" : 7, "resolution" : 1222.99245256249, "scale" : 4622324.434309}, 
                    //{"level" : 8, "resolution" : 611.49622628138, "scale" : 2311162.217155}, 
                    //{"level" : 9, "resolution" : 305.748113140558, "scale" : 1155581.108577}, 
                    {"level" : 10, "resolution" : 152.874056570411, "scale" : 577790.554289}, 
                    //{"level" : 11, "resolution" : 76.4370282850732, "scale" : 288895.277144}, 
                    {"level" : 12, "resolution" : 38.2185141425366, "scale" : 144447.638572}, 
                    //{"level" : 13, "resolution" : 19.1092570712683, "scale" : 72223.819286},
                    //{"level": 14, "resolution": 9.55462853563415, "scale": 36111.909643}, 
                    //{"level": 15, "resolution": 4.77731426794937, "scale": 18055.954822}, 
                    //{"level": 16, "resolution": 2.38865713397468, "scale": 9027.977411}, 
                    //{"level": 17, "resolution": 1.19432856685505, "scale": 4513.988705}
                    ];

                var fl = new FeatureLayer("someServer/arcgis/rest/services/someProject/FeatureServer/1", {
                    mode: FeatureLayer.MODE_SNAPSHOT,
                            displayLevels:[0,1],
                    outFields:["*"],
                    //infoTemplate: template,
                    visible: true,
                    value: "OBJECTID",
                }); 

                window.map = new Map("map", {
                    basemap:"topo",
                    center: [48.04, 28.50],
                    zoom: 0,
                    lods: lods,
                });

                var overviewMapDijit = new OverviewMap({
                    map: window.map,
                    visible: true
                });

                overviewMapDijit.startup();

                var updateEnd = fl.on("update-end", function() {
                    updateEnd.remove();
                    updateRenderer();
                    updateLables();
                });

                window.map.addLayers([fl]);

                function updateRenderer(){
                    fl.setRenderer(new SimpleRenderer(null));
                    var kachelnamen = new JsonRest({
                        target: "someTargetURL",
                        idAttribute: "WA",
                        headers: { "X-Requested-With": "" },
                     });
                     kachelnamen.query(null).then(drawFeatureLayer, kachelnamenError);
                     console.debug(kachelnamen);
                     console.info(kachelnamen);
                }

 ////////////////////////////////////////////////        
                 function updateLables(){
                    var font = new Font("11px", Font.STYLE_NORMAL,     Font.VARIANT_NORMAL, Font.WEIGHT_BOLDER);
                    arr.forEach(fl.graphics, function(graphic){
                        var centerPoint;
                        switch (graphic.geometry.type) {
                            case "point":
                            centerPoint = graphic.geometry;
                            break;
                            case "extent":
                            centerPoint = graphic.getCenter();
                            default:
                            centerPoint = graphic._extent.getCenter();
                        }
                        var textSymbol = new TextSymbol(graphic.attributes.NAME,font, new Color([0, 0, 0]));
                        var labelPointGraphic = new Graphic(centerPoint, textSymbol);
                        window.map.graphics.add(labelPointGraphic);
                    });
                }
 ///////////////////////////////////////////////////////////


                function drawFeatureLayer(data) {
                // data comes back as text from apify...parse it
                // loop through gas price data, find min/max and populate an object 
                // to keep track of the price of regular in each state
                    window.statekachelnamen = {};
                    var gasMin = Infinity;
                    var gasMax = -Infinity;
                    arr.forEach(data, function(g) {
                        var price = parseFloat(g.STAT_CALC);
                        statekachelnamen[g.WA] = price;
                        if ( price < gasMin ) {
                            gasMin = price;
                        }
                        if ( price > gasMax ) {
                            gasMax = price;
                        }
                    });

                    // format max gas price with two decimal places
                    gasMax = formatDollars(gasMax);
                    // add an attribute to each attribute so gas price is displayed
                    // on mouse over below the legend
                    arr.forEach(fl.graphics, function(g) {
                          //var displayValue = statekachelnamen[g.attributes.WA_NAME].toFixed(2);
                        var displayValue;
                        if (g.attributes.WA_NAME in statekachelnamen){
                            displayValue = statekachelnamen[g.attributes.WA_NAME].toFixed(2);
                        }
                        else{
                            displayValue = 0;
                        }
                        g.attributes.STAT_CALC = displayValue;
                    });

                    // create a class breaks renderer
                    var breaks = calcBreaks(gasMin, gasMax, 4);
                    //gasMax = formatDollars(gasMax);
                    // console.log("gas price breaks: ", breaks);
                    var sfs = SimpleFillSymbol;
                    var sls = SimpleLineSymbol;
                    var outline = sls("solid", new Color("#444"), 1);
                    var br = new ClassBreaksRenderer(null, findGasPrice);
                    br.setMaxInclusive(true);
                    br.addBreak(breaks[0], breaks[1], new sfs("solid", outline, new Color([255, 255, 178, 0.75])));
                    br.addBreak(breaks[1], breaks[2], new sfs("solid", outline, new Color([254, 204, 92, 0.75])));
                    br.addBreak(breaks[2], breaks[3], new sfs("solid", outline, new Color([253, 141, 60, 0.75])));
                    br.addBreak(breaks[3], gasMax, new sfs("solid", outline, new Color([227, 26, 28, 0.75])));

                    fl.setRenderer(br);
                    fl.redraw();

                    var legend2 = new Legend({
                      map: window.map,
                      layerInfos: [{ "layer": fl, "title": "WA Statistic" }]
                    },"legendDiv2");
                    legend2.startup();

                    // remove the loading div
                    //domConstruct.destroy("loading");

                }
      // function used by the class breaks renderer to get the
      // value used to symbolize each state
                function findGasPrice(graphic) {
                    var state = graphic.attributes.WA_NAME;
                    return statekachelnamen[state];
                    console.info(state);
                }

                function calcBreaks(min, max, numberOfClasses) {
                    var range = (max - min) / numberOfClasses;
                    var breakValues = [];
                    for ( var i = 0; i < numberOfClasses; i++ ) {
                        breakValues[i] = formatDollars(min + ( range * i ));
                    }
                    // console.log("break values: ", breakValues)
                    return breakValues;
                }

                var myButton = new Button({
                    label: "Update Renderer!",
                    onClick: function(){
                        updateRenderer();
                    }
                }, "progButtonNode");

                function formatDollars(num) {
                    return num.toFixed(2);
                    //return number.format(num, { "places": 2 });
                }

                function kachelnamenError(e) {
                    console.log("error getting gas price data: ", e);
                }
            }
        });
 });

      ![here the text is correct][1]
      ![here the text is correct too ][2]
      ![I don't want to display text here][3]

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决问题的方法。 这是它

                window.map.addLayers([fl]);
                var updateEnd = fl.on("update-end", function() {
                    updateEnd.remove();
                    updateRenderer();
                    updateLables();
                //show or hide the SWA text when the map's extent changes 
                    window.map.on("extent-change", function(evt) {
                        var currentScale = map.getScale();
                        console.log('maponScale: ' + currentScale);
                        if (currentScale.toString() == "4622324.434309"){
                            window.map.graphics.hide();
                        }
                        else{
                            window.map.graphics.show();
                        }
                    });
                });