描述
本例在开始和地图交互前执行一个查询任务并加在查询结果。这允许用户鼠标悬停在任意郡县时立即见到InfoWindow。本例改变了信息窗口的默认行为,仅在鼠标单击时显示信息窗口。
QueryTask构造函数接受被查询的图层,即 ESRI sample server上ESRI_Census_USA服务的郡县图层(索引号是3)。 注意这和地图使用的不是一个服务。地图使用来自 ArcGIS Online的ESRI_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);
});
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 6 <meta http-equiv="X-UA-Compatible" content="IE=7" /> 7 <title>QueryTask with geometry, results as an InfoWindow onHover</title> 8 <link rel="stylesheet" type="text/css" 9 href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css"> 10 <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script> 11 <script type="text/javascript" language="Javascript"> 12 dojo.require("esri.map"); 13 dojo.require("esri.tasks.query"); 14 15 function init() { 16 var startExtent = new esri.geometry.Extent(-83.41, 31.98, -78.47, 35.28, 17 new esri.SpatialReference({wkid:4326})); 18 //创建地图 19 var map = new esri.Map("mapDiv",{extent:startExtent}); 20 //加载地图添加查询功能 21 dojo.connect(map, "onLoad", initFunctionality); 22 23 //创建并添加层 24 var layer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"); 25 map.addLayer(layer); 26 } 27 28 function initFunctionality(map) { 29 //建立查询任务 30 var queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3"); 31 32 //创建查询过滤器 33 var query = new esri.tasks.Query(); 34 query.returnGeometry = true; 35 query.outFields = ["NAME", "POP2000", 36 "POP2007", "POP00_SQMI", "POP07_SQMI"]; 37 query.where = "STATE_NAME = 'South Carolina'"; 38 39 var infoTemplate = new esri.InfoTemplate(); 40 infoTemplate.setTitle("${NAME}"); 41 infoTemplate.setContent("<b>2000 Population: </b>${POP2000}<br/>" 42 + "<b>2000 Population per Sq. Mi.: </b>${POP00_SQMI}<br/>" 43 + "<b>2007 Population: </b>${POP2007}<br/>" 44 + "<b>2007 Population per Sq. Mi.:</b>${POP07_SQMI}"); 45 46 map.infoWindow.resize(245,125); 47 48 //监听onComplete事件的处理结果,使用queryTask.execute方法回调 49 dojo.connect(queryTask, "onComplete", function(featureSet) { 50 map.graphics.clear(); 51 var highlightSymbol = new esri.symbol.SimpleFillSymbol( 52 esri.symbol.SimpleFillSymbol.STYLE_SOLID, 53 new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, 54 new dojo.Color([255,0,0]), 3), 55 new dojo.Color([125,125,125,0.35]) 56 ); 57 //var highlightSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 3); 58 var symbol = new esri.symbol.SimpleFillSymbol( 59 esri.symbol.SimpleFillSymbol.STYLE_SOLID, 60 new esri.symbol.SimpleLineSymbol( 61 esri.symbol.SimpleLineSymbol.STYLE_SOLID, 62 new dojo.Color([255,255,255,0.35]), 63 1), 64 new dojo.Color([125,125,125,0.35]) 65 ); 66 67 var countiesGraphicsLayer = new esri.layers.GraphicsLayer(); 68 //QueryTask返回featureSet类型.通过featureSet的循环把他们添加到信息窗口 69 for (var i=0, il=featureSet.features.length; i<il; i++) { 70 //从featureSet中得到当前实例. 71 //从当前实例赋值给graphic 72 var graphic = featureSet.features[i]; 73 graphic.setSymbol(symbol); 74 graphic.setInfoTemplate(infoTemplate); 75 76 //添加当前这个图形到地图图层中. 77 countiesGraphicsLayer.add(graphic); 78 } 79 map.addLayer(countiesGraphicsLayer); 80 map.graphics.enableMouseEvents(); 81 //监听onMouseOver事件执行countiesGraphicsLayer 82 //监听onMouseOver事件并创建一个新的几何图形event.graphic到地图图形层 83 dojo.connect(countiesGraphicsLayer, "onMouseOver", function(evt) { 84 map.graphics.clear(); //使用地图图形层 85 var content = evt.graphic.getContent(); 86 map.infoWindow.setContent(content); 87 var title = evt.graphic.getTitle(); 88 map.infoWindow.setTitle(title); 89 var highlightGraphic = new esri.Graphic(evt.graphic.geometry,highlightSymbol); 90 map.graphics.add(highlightGraphic); 91 map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint)); 92 }); 93 94 //监听map.graphics onMouseOut事件时,清除显示的图形 95 //隐藏信息窗口 96 dojo.connect(map.graphics, "onMouseOut", function(evt) { 97 map.graphics.clear(); 98 map.infoWindow.hide(); 99 }); 100 }); 101 102 queryTask.execute(query); 103 } 104 dojo.addOnLoad(init); 105 </script> 106 </head> 107 <body class="tundra"> 108 Hover over a county in South Carolina to get more information. 109 <div id="mapDiv" style="900px; height:600px; border:1px solid #000;"></div> 110 </body> 111 </html>