ArcGIS api for JavaScript 3.27 FindTask查询功能

在ArcGIS API中查询功能是经常使用的,常用的三个查询分别是FindTask,QueryTask,IdentifyTask。它们各自都有自己的特点。

查询功能分为属性查询和空间查询

 

FindTask的官方API链接:

https://developers.arcgis.com/javascript/3/jsapi/findtask-amd.html

这个demo是查询某一个图层的一个属性,然后把结果撒点在地图上,鼠标移上去可以看到自定的infowindow

function doFind() {
    // alert("QQQ");
    require(
        [
            "esri/tasks/FindTask", "esri/tasks/FindParameters",
            "esri/SpatialReference",
            "esri/InfoTemplate",
            "esri/geometry/Point",
            "esri/graphic",
            "esri/layers/GraphicsLayer",
            "esri/geometry/webMercatorUtils",
            "esri/symbols/SimpleFillSymbol",
            "esri/symbols/SimpleLineSymbol",
            "esri/symbols/SimpleMarkerSymbol",
            "js/PointCluster",
            "dojo/dom"
        ],function (
            FindTask,
            FindParameters,
            SpatialReference,
            InfoTemplate,
            Point,
            Graphic,
            GraphicsLayer,
            webMercatorUtils,
            SimpleFillSymbol,
            SimpleLineSymbol,
            SimpleMarkerSymbol,
            PointCluster,
            dom,
        ) {
            //要查询的数据源
            var findTask = new FindTask("http://localhost:6080/arcgis/rest/services/GW_MAP_LF/MapServer");
            //创建属性查询参数
            var findParams = new FindParameters();
            // findParams.returnGeometry = true;
            //查询所有图层
            findParams.layerIds = [23];
            //查询指定的字段,如果不指定此参数,则查询所有的字段
            findParams.searchFields = ["name"];
            //是否返回几何对象
            // findParams.returnGeometry = true;
            //是否接受模糊查找,如果仅仅进行模糊查询,不需要where,设置这个参数就可以
            // findParams.contains = true;
            findParams.searchText = dom.byId("text").value;
            if(map.graphicsLayerIds)
            {
                //map.infoWindow.hide();
                clusterLayer.hide();
            }
            //执行查询
            findTask.execute(findParams,function(results){

                if(results){
                    console.log(results);

                    // var json = {title:"Attributes",content:"company: ${layerId}"};
                    var infoTemplate = new InfoTemplate();
                    //map.infoWindow.resize(width,height)
                    //map.infoWindow.resize(245,125);

                    /*地图显示样式*/
                    var graphicLayer = new GraphicsLayer();
                    var defaultsymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_SQUARE, 10,
                        new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
                            new dojo.Color([255,0,0]), 1),
                        new dojo.Color([0,255,0,0.25]));

                    for (var i = 0; i < results.length; i++) {
                        var pt = new Point(results[i].feature.attributes.x,results[i].feature.attributes.y,map.spatialReference);
                        infoTemplate.title="Attributes";
                        infoTemplate.content=results[i].feature.attributes.y;
                        var graphic = new Graphic(pt,defaultsymbol,null,infoTemplate);
                        graphicLayer.add(graphic);
                    }
                    map.addLayer(graphicLayer);

                    graphicLayer.on("mouse-over", 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 Graphic(evt.graphic.geometry,defaultsymbol);
                        map.graphics.add(highlightGraphic);
                        map.infoWindow.show(evt.screenPoint,map.getInfoWindowAnchor(evt.screenPoint));
                    });
                    graphicLayer.on("mouse-out", function (evt) {
                        // map.graphics.clear();
                        // map.infoWindow.hide();
                    });
                }else{
                    alert("查不到");
                }
            });
        });
}

 

posted @ 2019-05-07 20:08  昜木辰deブログ  阅读(893)  评论(0编辑  收藏  举报