ArcGIS api for javascript——加载查询结果,悬停显示信息窗口

转自原文 ArcGIS api for javascript——加载查询结果,悬停显示信息窗口

描述

本例在开始和地图交互前执行一个查询任务并加在查询结果。这允许用户鼠标悬停在任意郡县时立即见到InfoWindow。本例改变了信息窗口的默认行为,仅在鼠标单击时显示信息窗口。

QueryTask构造函数接受被查询的图层,即 ESRI sample serverESRI_Census_USA服务的郡县图层(索引号是3)。 注意这和地图使用的不是一个服务。地图使用来自 ArcGIS OnlineESRI_StreetMap_World_2D服务。 通过这种方式能够使用在视觉上匹配的不同服务作为地图和查询任务的服务。 注意本例中,街道地图被显示在没有郡县可见的比例,避免查询任务返回结果和郡县要素视觉冲突的可能性。

查询的where clause限制了结果是在美国南卡罗来纳州的郡县。还要注意 returnGeometry被设置为true以便要素能够被显示在地图上。

//build query filter
var query = new esri.tasks.Query();
query.returnGeometry = true;
query.outFields = ["NAME", "POP2000", "POP2007", "POP00_SQMI", "POP07_SQMI"];
query.where = "STATE_NAME = 'South Carolina'";

InfoTemplate定义了在信息窗口中属性信息如何被格式化。注意在信息模板内容中能使用HTML。同样,能使用语法${fieldName} 来显示给出记录的字段的值。例如: ${POP2000}.

查询任务返回一个FeatureSet,要素集合是一个包含每个郡县图形的数组。下面的循环给每个图形分配一个 Symbol和信息模板,然后把图形增加到地图上:

for (var i=0, il=featureSet.features.length; i<il; i++) {
          
 var graphic = featureSet.features[i];
 graphic.setSymbol(symbol);
 graphic.setInfoTemplate(infoTemplate);
 map.graphics.add(graphic);
}

当鼠标悬停在图形上时,下面的事件监听器显示信息窗口并且修改符号:

dojo.connect(map.graphics, "onMouseOver", function(evt) {
  ...
  evt.graphic.setSymbol(highlightSymbol);
  map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint));
});

最后,当鼠标移出图形时,这个监听器是需要删除图形:

dojo.connect(map.graphics, "onMouseOut", function(evt) {
   map.infoWindow.hide();
   evt.graphic.setSymbol(symbol);
 });

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <title>QueryTask with geometry, results as an InfoWindow onHover</title>
    <link rel="stylesheet" type="text/css"
    href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script>
    <script type="text/javascript" language="Javascript">
      dojo.require("esri.map");
      dojo.require("esri.tasks.query");

      function init() {
        var startExtent = new esri.geometry.Extent(-83.41, 31.98, -78.47, 35.28,
        new esri.SpatialReference({wkid:4326}));
        //创建地图
        var map = new esri.Map("mapDiv",{extent:startExtent});
        //加载地图添加查询功能
        dojo.connect(map, "onLoad", initFunctionality);

        //创建并添加层
        var layer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");
        map.addLayer(layer);
      }

      function initFunctionality(map) {
        //建立查询任务
        var queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3");

        //创建查询过滤器
        var query = new esri.tasks.Query();
        query.returnGeometry = true;
        query.outFields = ["NAME", "POP2000",
        "POP2007", "POP00_SQMI", "POP07_SQMI"];
        query.where = "STATE_NAME = 'South Carolina'";

        var infoTemplate = new esri.InfoTemplate();
        infoTemplate.setTitle("${NAME}");
        infoTemplate.setContent("<b>2000 Population: </b>${POP2000}<br/>"
                             + "<b>2000 Population per Sq. Mi.: </b>${POP00_SQMI}<br/>"
                             + "<b>2007 Population: </b>${POP2007}<br/>"
                             + "<b>2007 Population per Sq. Mi.:</b>${POP07_SQMI}");

        map.infoWindow.resize(245,125);

        //监听onComplete事件的处理结果,使用queryTask.execute方法回调
        dojo.connect(queryTask, "onComplete", function(featureSet) {
              map.graphics.clear();
              var highlightSymbol = new esri.symbol.SimpleFillSymbol(
                  esri.symbol.SimpleFillSymbol.STYLE_SOLID,
                  new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
                  new dojo.Color([255,0,0]), 3), 
                  new dojo.Color([125,125,125,0.35])
              );
          //var highlightSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 3);
          var symbol = new esri.symbol.SimpleFillSymbol(
                  esri.symbol.SimpleFillSymbol.STYLE_SOLID,
                    new esri.symbol.SimpleLineSymbol(
                        esri.symbol.SimpleLineSymbol.STYLE_SOLID, 
                        new dojo.Color([255,255,255,0.35]), 
                        1),
                new dojo.Color([125,125,125,0.35])
                );

          var countiesGraphicsLayer = new esri.layers.GraphicsLayer();
          //QueryTask返回featureSet类型.通过featureSet的循环把他们添加到信息窗口
          for (var i=0, il=featureSet.features.length; i<il; i++) {
            //从featureSet中得到当前实例.
            //从当前实例赋值给graphic
            var graphic = featureSet.features[i];
            graphic.setSymbol(symbol);
            graphic.setInfoTemplate(infoTemplate);

            //添加当前这个图形到地图图层中.
            countiesGraphicsLayer.add(graphic);
          }
          map.addLayer(countiesGraphicsLayer);
          map.graphics.enableMouseEvents();
          //监听onMouseOver事件执行countiesGraphicsLayer
          //监听onMouseOver事件并创建一个新的几何图形event.graphic到地图图形层
          dojo.connect(countiesGraphicsLayer, "onMouseOver", function(evt) {
            map.graphics.clear();  //使用地图图形层
            var content = evt.graphic.getContent();
            map.infoWindow.setContent(content);
            var title = evt.graphic.getTitle();
            map.infoWindow.setTitle(title);
            var highlightGraphic = new esri.Graphic(evt.graphic.geometry,highlightSymbol);
            map.graphics.add(highlightGraphic);
            map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint));
          });

          //监听map.graphics onMouseOut事件时,清除显示的图形
          //隐藏信息窗口
          dojo.connect(map.graphics, "onMouseOut", function(evt) {
            map.graphics.clear();
            map.infoWindow.hide();
          });
        });

        queryTask.execute(query);
      }
      dojo.addOnLoad(init);
    </script>
  </head>
  <body class="tundra">
    Hover over a county in South Carolina to get more information.
    <div id="mapDiv" style="width:900px; height:600px; border:1px solid #000;"></div>
  </body>
</html>
View Code

 




posted @ 2017-08-29 14:06  wenglabs  阅读(542)  评论(0编辑  收藏  举报