首页 > 代码库 > 利用 Task\Query 实现定位 , FeatureLayer 的属性查询

利用 Task\Query 实现定位 , FeatureLayer 的属性查询

放纵了几天,又有了学习的动力。今天实现了利用对 FeatureLayer 进行属性查询在地图上进行跳转。

  一、我下载了一幅浙江省的县界面地图,存在NAME字段,利用Name就能进行查询了:

var name = tb.value;if(name!=""){    var query = new esri.tasks.Query();    query.where = "NAME = ‘"+name+"‘";    fl.queryFeatures(query, function (featureset) {        var a =featureset.features[0].geometry.getExtent();        map.setExtent(a);    });}

  备注:FeatureLayer.queryFeatures 返回的不是 Feature 的一个集合,需要注意的是它本身就是单独的一个 Object ,而不是单纯的 Arry[] ,一开始没仔细看,半天取不出数据。不知道能不能进行模糊查询,希望可以,不然真的不方便啊。再试试...

  二、很简单的例子给大家看看:

<!DOCTYPE html><html>    <head>        <meta charset="utf-8">        <title>Query Demo</title>        <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/dojo/dijit/themes/tundra/tundra.css">        <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">        <script type="text/javascript" charset="utf-8" src="http://js.arcgis.com/3.10/"></script>        <script type="text/javascript" charset="utf-8" src="js/dojo.js"></script>                <script type="text/javascript" charset="utf-8">          require([            "esri/map", "esri/layers/FeatureLayer",            "esri/tasks/query", "esri/TimeExtent","esri/tasks/query",            "dojo/number", "dojo/date/locale", "dojo/dom","dojo/on",            "dojo/_base/array", "dojo/store/Memory",            "dgrid/OnDemandGrid", "dojo/domReady!"          ], function()          {              //初始化地图控件              var map = new esri.Map("mapdiv");              map.setBasemap("osm");            var bounds = new esri.geometry.Extent({              "xmin":118.363523,"ymin":29.219302,"xmax":119.508848,"ymax":30.073848,              "spatialReference":{"wkid":4326}});            map.setExtent(bounds);            //初始化图层            var durl = "http...MapServer";            var dl = new esri.layers.ArcGISDynamicMapServiceLayer(durl);            var furl = "http...MapServer/0";            var fl = new esri.layers.FeatureLayer(furl,{outFields: ["*"],opacity: 0});            map.addLayer(dl);            map.addLayer(fl);            //注册Go点击事件            var btn = document.getElementById("query");              btn.onclick = function(){                  var tb = document.getElementById("val");                    var name = tb.value;                    if(name!="")                    {                        var query = new esri.tasks.Query();                        query.where = "NAME = ‘"+name+"";                        fl.queryFeatures(query, function (featureset) {                            var a =featureset.features[0].geometry.getExtent();                            map.setExtent(a);                        });                    }              }          });        </script>    </head>    <body>        <div id="tolbox">            Name:            <input type="text" name="val" id="val" />            <input type="button" name="query" id="query" value="Go" />        </div>        <div id="mapdiv" style="width: 600px; height: 600px; background-color: #FF8F8F;"></div>    </body></html>

效果: