如何在if else语句中访问$ NAME变量?

时间:2015-06-24 05:41:37

标签: javascript html gis arcgis-js-api

您可以在下面的链接中找到代码。

Sample running Code

infoTemplate中的

${NAME}显示县名。

我想添加一个显示县名的警告框。我可以创建一个警告框,但不能显示县名。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

好吧,要显示警报,您必须在功能上附加点击事件,或者您必须使用任何事件来传播警报消息。 在上面的示例网址中,我使用要素图层点击事件在警报中显示“NAME”。

以下是工作代码:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Class Breaks Renderer</title>

    <link rel="stylesheet" href="http://js.arcgis.com/3.13/esri/css/esri.css">
    <style>
      html, body, #map{
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
    <script src="http://js.arcgis.com/3.13/"></script>
    <script>
      var map;
      require([
        "esri/map", "esri/layers/FeatureLayer",
        "esri/InfoTemplate", "esri/symbols/SimpleFillSymbol", 
        "esri/renderers/ClassBreaksRenderer",
        "esri/Color", "dojo/dom-style", "dojo/on", "dojo/domReady!"
      ], function(
        Map, FeatureLayer,
        InfoTemplate, SimpleFillSymbol, 
        ClassBreaksRenderer,
        Color, domStyle, on
      ) {
        map = new Map("map", {
          basemap: "streets",
          center: [-98.215, 38.382],
          zoom: 7,
          slider: false
        });

        var symbol = new SimpleFillSymbol();
        symbol.setColor(new Color([150, 150, 150, 0.5]));

        // Add five breaks to the renderer.
        // If you have ESRI's ArcMap available, this can be a good way to determine break values.
        // You can also copy the RGB values from the color schemes ArcMap applies, or use colors
        // from a site like www.colorbrewer.org
        //
        // alternatively, ArcGIS Server's generate renderer task could be used
        var renderer = new ClassBreaksRenderer(symbol, "POP07_SQMI");
        renderer.addBreak(0, 25, new SimpleFillSymbol().setColor(new Color([56, 168, 0, 0.5])));
        renderer.addBreak(25, 75, new SimpleFillSymbol().setColor(new Color([139, 209, 0, 0.5])));
        renderer.addBreak(75, 175, new SimpleFillSymbol().setColor(new Color([255, 255, 0, 0.5])));
        renderer.addBreak(175, 400, new SimpleFillSymbol().setColor(new Color([255, 128, 0, 0.5])));
        renderer.addBreak(400, Infinity, new SimpleFillSymbol().setColor(new Color([255, 0, 0, 0.5])));

        var infoTemplate = new InfoTemplate("${NAME}", "${*}");
        var featureLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3", {
          mode: FeatureLayer.MODE_SNAPSHOT,
          outFields: ["*"],
          infoTemplate: infoTemplate
        });

        featureLayer.setDefinitionExpression("STATE_NAME = 'Kansas'");
        featureLayer.setRenderer(renderer);
        map.addLayer(featureLayer);
        on(featureLayer, "click", function(evt){
          alert(evt.graphic.attributes.NAME)
        });
      });
    </script>
  </head>

  <body>
    <div id="map"></div>
  </body>

</html>

在这段代码中,我使用了上面的示例代码和特征层点击事件。

希望这会对你有所帮助:)。

如果需要澄清,请告诉我们。

答案 1 :(得分:0)

我们可以通过以下方式访问infotemplate外的$ NAME ... var t =“ $ {NAME}


事件:”; + inc //字符串替换方法 var content = esriLang.substitute(evt.graphic.attributes,t);