Guest User

arcGIS webmap example hybrid

a guest
Jan 31st, 2013
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //req for layout
  3. dojo.require("dijit.layout.BorderContainer");
  4. dojo.require("dijit.layout.ContentPane");
  5. dojo.require("dijit.layout.AccordionContainer");
  6.  
  7.  
  8. //req for map to show up and layers
  9. dojo.require("esri.map");
  10. dojo.require("esri.layers.FeatureLayer");
  11.  
  12.  
  13. //req for measurement widget
  14. dojo.require("dijit.form.CheckBox");
  15. dojo.require("esri.dijit.Scalebar");
  16. dojo.require("esri.dijit.Measurement");
  17. dojo.require("esri.SnappingManager");
  18.  
  19.  
  20. //edit widget
  21. dojo.require("dijit.Toolbar");
  22. dojo.require("esri.dijit.editing.Editor-all");
  23.  
  24.  
  25. //legend widget
  26. dojo.require("esri.dijit.Legend");
  27.  
  28.  
  29. dojo.require("dojox.grid.DataGrid");
  30. dojo.require("dojo.data.ItemFileReadStore");
  31. dojo.require("esri.tasks.find");
  32.  
  33.  
  34. var map;
  35. var editorWidget;
  36. var legendLayers = [];
  37. var findTask, findParams;
  38.  
  39.  
  40.  
  41. function init() {
  42.  
  43.     //necessary for measurement widget!
  44.     esri.config.defaults.io.proxyUrl = "/proxy.ashx";
  45.     esri.config.defaults.io.alwaysUseProxy = false;
  46.     esri.config.defaults.geometryService = new esri.tasks.GeometryService("http://ingisd01/ArcGIS/rest/services/Geometry/GeometryServer");
  47.  
  48.  
  49.     //create a map
  50.     map = new esri.Map("map", {
  51.         zoom: 1
  52.     });
  53.  
  54.  
  55.     //layers
  56.     var baselayer = new esri.layers.ArcGISTiledMapServiceLayer("http://ingisd01/ArcGIS/rest/services/Tokanui/tokanui_imagery_nztm/MapServer");
  57.     map.addLayer(baselayer);
  58.     paddocks = new esri.layers.FeatureLayer("http://ingisd01/arcgis/rest/services/Tokanui/tokanui_paddock_test/FeatureServer/0", { outFields: ['*'] });
  59.    
  60.     soils = new esri.layers.ArcGISDynamicMapServiceLayer("http://ingisd01/ArcGIS/rest/services/Tokanui/tokanui_soils_nztm/MapServer");
  61.     map.addLayer(soils);
  62.     legendLayers.push({ layer: paddocks, title: 'Paddocks' });
  63.     legendLayers.push({ layer: soils, title: 'Soils' });
  64.  
  65.  
  66.  
  67.  
  68.     //resize with browser
  69.     dojo.connect(map, 'onLoad', function () {
  70.         //resize the map when the browser resizes
  71.         dojo.connect(dijit.byId('map'), 'resize', map, map.resize);
  72.     });
  73.  
  74.  
  75.     //after map loads, connect to listen to mouse move & drag events
  76.     dojo.connect(map, "onLoad", function () {
  77.         dojo.connect(map, "onMouseMove", showCoordinates);
  78.         dojo.connect(map, "onMouseDrag", showCoordinates);
  79.         dojo.connect(map, "onClick", addPoint);
  80.     });
  81.  
  82.  
  83.     //Measurement widget colour of lines
  84.     var sfs = new esri.renderer.SimpleRenderer(new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID,
  85.              new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
  86.              new dojo.Color([255, 0, 0]), 2), new dojo.Color([180,180,180,0.15]), null));
  87.     paddocks.setRenderer(sfs);
  88.  
  89.  
  90.     //starts measurement widget, snaps to 'paddocks' layer and places widget in 'measurementDiv'
  91.     dojo.connect(map, "onLayersAddResult", function () {
  92.         var snapManager = map.enableSnapping({ snapKey: dojo.keys.copyKey });
  93.         var layerInfos = [{ layer: paddocks }];
  94.         snapManager.setLayerInfos(layerInfos);
  95.  
  96.         var measurement = new esri.dijit.Measurement({
  97.             map: map
  98.         }, dojo.byId('measurementDiv'));
  99.         measurement.startup();
  100.     });
  101.    
  102.    
  103.  
  104.     dojo.connect(map, 'onLayersAddResult', function (results) {
  105.             var legendDijit = new esri.dijit.Legend({
  106.                 map: map,
  107.                 layerInfos: legendLayers
  108.             }, "legendDiv");
  109.             legendDijit.startup();
  110.         }
  111.     );
  112.     map.addLayers([paddocks]);
  113.  
  114.  
  115.     dojo.connect(map, "onLayersAddResult", initEditor);
  116.    
  117.  
  118.     //create find task with url to map service
  119.     findTask = new esri.tasks.FindTask("http://ingisd01/ArcGIS/rest/services/Tokanui/tokanui_paddock_test/FeatureServer");
  120.  
  121.     //create find parameters and define known values
  122.     findParams = new esri.tasks.FindParameters();
  123.     findParams.returnGeometry = true;
  124.     findParams.layerIds = [0];
  125.     findParams.searchFields = ["PadID","Caption"];
  126. }
  127.  
  128.  
  129.  
  130. function execute(searchText) {
  131.     //set the search text to find parameters
  132.     findParams.searchText = searchText;
  133.     findTask.execute(findParams, showResults);
  134. }
  135.  
  136. function showResults(results) {
  137.     //symbology for graphics
  138.     var markerSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 1), new dojo.Color([0, 255, 0, 0.25]));
  139.     var lineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASH, new dojo.Color([255, 0, 0]), 1);
  140.     var polygonSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_NONE, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color([255, 0, 0]), 2), new dojo.Color([255, 255, 0, 0.25]));
  141.  
  142.     //find results return an array of findResult.
  143.     map.graphics.clear();
  144.     var dataForGrid = [];
  145.     //Build an array of attribute information and add each found graphic to the map
  146.     dojo.forEach(results, function (result) {
  147.         var graphic = result.feature;
  148.         dataForGrid.push([result.layerName, result.foundFieldName, result.value]);
  149.         switch (graphic.geometry.type) {
  150.             case "point":
  151.                 graphic.setSymbol(markerSymbol);
  152.                 break;
  153.             case "polyline":
  154.                 graphic.setSymbol(lineSymbol);
  155.                 break;
  156.             case "polygon":
  157.                 graphic.setSymbol(polygonSymbol);
  158.                 break;
  159.         }
  160.         map.graphics.add(graphic);
  161.     });
  162.     var data = {
  163.         items: dataForGrid
  164.     };
  165.     var store = new dojo.data.ItemFileReadStore({
  166.         data: data
  167.     });
  168.     grid.setStore(store);
  169. }
  170.  
  171.  
  172.  
  173. //mouse coordinates
  174. function showCoordinates(evt) {
  175.     //get mapPoint from event
  176.     //The map is in web mercator - modify the map point to display the results in geographic
  177.     var mp = esri.geometry.webMercatorToGeographic(evt.mapPoint);
  178.     //display mouse coordinates
  179.     dojo.byId("info3").innerHTML = mp.x.toFixed(3) + ", " + mp.y.toFixed(3);
  180. }
  181.  
  182.  
  183. //previous click coordinates
  184. function addPoint(evt) {
  185.     map.infoWindow.setTitle("Coordinates");
  186.     //Need to convert the coordinates from the map's spatial reference (web mercator) to geographic to display lat/lon values
  187.     var geoPt = esri.geometry.webMercatorToGeographic(evt.mapPoint);
  188.     dojo.byId("info2").innerHTML = "lat/lon : " + geoPt.y.toFixed(2) + ", " + geoPt.x.toFixed(2) +
  189.       "<br />screen x/y : " + evt.screenPoint.x + ", " + evt.screenPoint.y;
  190. }
  191.  
  192.  
  193. function initEditor(results) {
  194.  
  195.     //build the layer and field information for the layer, display the description field using a text area.
  196.     var layers = dojo.map(results, function (result) {
  197.         var fieldInfos = dojo.map(result.layer.fields, function (field) {
  198.             if (field.name === 'Caption') {
  199.                 return { 'fieldName': 'Caption', 'label': 'Caption', stringFieldOption: esri.dijit.AttributeInspector.STRING_FIELD_OPTION_TEXTAREA };
  200.             }
  201.             else {
  202.                 return { 'fieldName': field.name, 'label': field.alias };
  203.             }
  204.         });
  205.         return { featureLayer: result.layer, 'fieldInfos': fieldInfos };
  206.     });
  207.  
  208.     var settings = {
  209.         map: map,
  210.         enableUndoRedo: true,
  211.         layerInfos: layers,
  212.         toolbarVisible: true,
  213.         createOptions: {
  214.             polygonDrawTools: [
  215.               esri.dijit.editing.Editor.CREATE_TOOL_FREEHAND_POLYGON,
  216.               esri.dijit.editing.Editor.CREATE_TOOL_AUTOCOMPLETE
  217.             ]
  218.         },
  219.         toolbarOptions: {
  220.             reshapeVisible: true,
  221.             cutVisible: true,
  222.             mergeVisible: true
  223.         }
  224.     };
  225.     var params = { settings: settings };
  226.  
  227.     editorWidget = new esri.dijit.editing.Editor(params, 'editorDiv');
  228.  
  229.  
  230.     //create a new checkbox to enable/disable snapping
  231.     var checkBox = new dijit.form.CheckBox({
  232.         name: "chkSnapping",
  233.         checked: true,
  234.         id: "chkSnapping",
  235.         label: "Snapping",
  236.         showLabel: "false",
  237.         title: "Snapping",
  238.         onChange: function (evt) {
  239.             console.log(this.checked);
  240.             if (this.checked) {
  241.                 map.enableSnapping({ snapKey: dojo.keys.copyKey });
  242.             } else {
  243.                 map.disableSnapping();
  244.             }
  245.         }
  246.     });
  247.  
  248.     //add the snapping checkbox to the editor's toolbar
  249.     var myToolbarElement = dojo.query(".esriDrawingToolbar", editorWidget.domNode)[0];
  250.     var myToolbar = dijit.byId(myToolbarElement.id);
  251.  
  252.     myToolbar.addChild(new dijit.ToolbarSeparator());
  253.     myToolbar.addChild(checkBox);
  254.  
  255.     editorWidget.startup();
  256.  
  257.     //listen for the template pickers onSelectionChange and disable
  258.     //the snapping checkbox when a template is selected
  259.     var templatePickerElement = dojo.query(".esriTemplatePicker", editorWidget.domNode)[0];
  260.     var templatePicker = dijit.byId(templatePickerElement.id);
  261.     dojo.connect(templatePicker, "onSelectionChange", function () {
  262.         if (templatePicker.getSelected()) {
  263.             //disable the snapping checkbox
  264.             dijit.byId('chkSnapping').set("disabled", true);
  265.         } else {
  266.             dijit.byId('chkSnapping').set("disabled", false);
  267.         }
  268.     });
  269.     map.infoWindow.resize(325, 200);
  270. }
  271.  
  272.  
  273. dojo.ready(init);
Advertisement
Add Comment
Please, Sign In to add comment