本节讲的是Arcgis for Js的针对graphiclayer的空间查询,内容很简单,代码如下:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/> <title>Simple Map</title> <link rel="stylesheet" href="http://localhost/arcgis_js_api/library/3.9/3.9/js/esri/css/esri.css"> <style> html, body, #map { height: 100%; margin: 0; padding: 0; } body { background-color: #FFF; overflow: hidden; font-family: "Trebuchet MS"; } #map_ctrl{ z-index: 99; position: absolute; top: 20pt; right: 10pt; background: #fff; } .button{ padding: 3px 8px; background: #eee; text-align: center; font-size: 12px; font-family: "微软雅黑"; } .button:hover,.attr_ctrl:hover{ background: #ccc; cursor: pointer; } </style> <script src="http://localhost/arcgis_js_api/library/3.9/3.9/init.js"></script> <script src="jquery-1.8.3.js"></script> <script> var iconPath = "M24.0,2.199C11.9595,2.199,2.199,11.9595,2.199,24.0c0.0,12.0405,9.7605,21.801,21.801,21.801c12.0405,0.0,21.801-9.7605,21.801-21.801C45.801,11.9595,36.0405,2.199,24.0,2.199zM31.0935,11.0625c1.401,0.0,2.532,2.2245,2.532,4.968S32.4915,21.0,31.0935,21.0c-1.398,0.0-2.532-2.2245-2.532-4.968S29.697,11.0625,31.0935,11.0625zM16.656,11.0625c1.398,0.0,2.532,2.2245,2.532,4.968S18.0555,21.0,16.656,21.0s-2.532-2.2245-2.532-4.968S15.258,11.0625,16.656,11.0625zM24.0315,39.0c-4.3095,0.0-8.3445-2.6355-11.8185-7.2165c3.5955,2.346,7.5315,3.654,11.661,3.654c4.3845,0.0,8.5515-1.47,12.3225-4.101C32.649,36.198,28.485,39.0,24.0315,39.0z"; var CITY_DATA={ "total":34, "items":[{"id":1,"name":"乌鲁木齐","X":87.575829,"Y":43.782212}, {"id":2,"name":"拉萨","X":91.162998,"Y":29.71042}, {"id":3,"name":"西宁","X":101.797303,"Y":36.593642}, {"id":4,"name":"兰州","X":103.584297,"Y":36.119086}, {"id":5,"name":"成都","X":104.035508,"Y":30.714179}, {"id":6,"name":"重庆","X":106.519115,"Y":29.478925}, {"id":7,"name":"贵阳","X":106.668071,"Y":26.457312}, {"id":8,"name":"昆明","X":102.726775,"Y":24.969385}, {"id":9,"name":"银川","X":106.167225,"Y":38.598524}, {"id":10,"name":"西安","X":108.967128,"Y":34.276112}, {"id":11,"name":"南宁","X":108.233931,"Y":22.748296}, {"id":12,"name":"海口","X":110.346181,"Y":19.96992}, {"id":13,"name":"广州","X":113.226683,"Y":23.18307}, {"id":14,"name":"长沙","X":112.947928,"Y":28.169916}, {"id":15,"name":"南昌","X":115.893715,"Y":28.652363}, {"id":16,"name":"福州","X":119.246768,"Y":26.070765}, {"id":17,"name":"台北","X":121.503567,"Y":25.008274}, {"id":18,"name":"杭州","X":120.183046,"Y":30.330584}, {"id":19,"name":"上海","X":121.449707,"Y":31.253361}, {"id":20,"name":"武汉","X":114.216597,"Y":30.579253}, {"id":21,"name":"合肥","X":117.262302,"Y":31.838353}, {"id":22,"name":"南京","X":118.805692,"Y":32.085022}, {"id":23,"name":"郑州","X":113.6511,"Y":34.746308}, {"id":24,"name":"济南","X":117.048331,"Y":36.60841}, {"id":25,"name":"石家","X":114.478215,"Y":38.033276}, {"id":26,"name":"太原","X":112.483066,"Y":37.798404}, {"id":27,"name":"呼和浩特","X":111.842806,"Y":40.895751}, {"id":28,"name":"天津","X":117.351094,"Y":38.925719}, {"id":29,"name":"沈阳","X":123.296299,"Y":41.801604}, {"id":30,"name":"长春","X":125.26142,"Y":43.981984}, {"id":31,"name":"哈尔","X":126.567138,"Y":45.69381}, {"id":32,"name":"北京","X":116.068276,"Y":39.892225}, {"id":33,"name":"香港","X":114.093117,"Y":22.427852}, {"id":34,"name":"澳门","X":113.552482,"Y":22.184495} ] }; var map, mapCenter; var drawToolbar, selectedGraphics=new Array(); require([ "esri/map", "esri/layers/ArcGISTiledMapServiceLayer", "esri/layers/FeatureLayer", "esri/layers/GraphicsLayer", "esri/geometry/Point", "esri/symbols/PictureMarkerSymbol", "esri/symbols/SimpleLineSymbol", "esri/renderers/SimpleRenderer", "dojo/_base/Color", "dojo/on", "dojo/dom", "dojo/domReady!"], function(Map, Tiled, FeatureLayer, GraphicsLayer, Point, PictureMarkerSymbol, SimpleLineSymbol, SimpleRenderer, Color, on, dom) { map = new Map("map", {logo:false,slider: true}); var tiled = new Tiled("http://localhost:6080/arcgis/rest/services/chinamap/MapServer"); map.addLayer(tiled,0); var chartLayer = new GraphicsLayer({"id":"chartLayer"}); map.addLayer(chartLayer,1); mapCenter = new Point(103.847, 36.0473, map.spatialReference); map.centerAndZoom(mapCenter,4); addReadPopup(CITY_DATA); //添加红气泡 function addReadPopup(data){ var items= data.items; for(var i=0;i<data.total;i++){ var pt=new Point(items[i].X,items[i].Y,map.spatialReference); var graphic = new esri.Graphic(pt,createSymbol(iconPath, "#ff0000"),items[i]); chartLayer.add(graphic); } }; function createSymbol(path, color){ var markerSymbol = new esri.symbol.SimpleMarkerSymbol(); markerSymbol.setPath(path); markerSymbol.setColor(new dojo.Color(color)); markerSymbol.setOutline(null); return markerSymbol; }; drawToolbar = new esri.toolbars.Draw(map); drawToolbar.on("draw-end",showResults); on(dom.byId("extent"), "click", function(){ map.setMapCursor("pointer"); for(var i= 0, total=selectedGraphics.length;i<total;i++){ selectedGraphics[i].symbol = createSymbol(iconPath,"#f00"); chartLayer.redraw(); } selectedGraphics=[]; drawToolbar.activate(esri.toolbars.Draw.EXTENT); }); function showResults(extent){ drawToolbar.deactivate(); map.setMapCursor("default"); var graphics = chartLayer.graphics; for(var i= 0, total=graphics.length;i<total;i++){ if(extent.geometry.contains(graphics[i].geometry)){ selectedGraphics.push(graphics[i]); graphics[i].symbol = createSymbol(iconPath,"#0ff"); chartLayer.redraw(); } } }; }); </script> </head> <body> <div id="map"> <div id="map_ctrl"> <a id="extent" class="button">矩 形</a> </div> </div> </body> </html>
实现后的效果如下:
初始状态
选择框