2015-07-03 26 views
0

我可以加載我的esri web地圖,但緩衝區無法工作。使用我的esri webmap使用ArcGIS API for JavaScript製作緩衝區

我用的樣品從developers.arcgis.com/javascript從此路徑:https://developers.arcgis.com/javascript/jssamples/util_buffergraphic.html

而且加入我的網絡地圖ID修改它,並做了一些改動。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
 
<!--The viewport meta tag is used to improve the presentation and behavior of the samples 
 
    on iOS devices--> 
 
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"> 
 
<title>Buffer</title> 
 

 
<link rel="stylesheet" href="http://js.arcgis.com/3.13/dijit/themes/claro/claro.css"> 
 
<link rel="stylesheet" href="http://js.arcgis.com/3.13/esri/css/esri.css"> 
 
<style> 
 
    html, body { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
    overflow:hidden; 
 
    } 
 
    #leftPane{ 
 
    color:#000; 
 
    width:250px; 
 
    padding-bottom:15px; 
 
    } 
 
    #map{ 
 
    padding:0; 
 
    } 
 
    .details{ 
 
    font-size:14px; 
 
    font-weight:600; 
 
    padding-bottom:20px; 
 
    } 
 

 
    button{ 
 
    margin:2px; 
 
    cursor:pointer; 
 
    } 
 
</style> 
 

 
<script src="http://js.arcgis.com/3.13/"></script> 
 
<script> 
 
var map, tb, 
 
     webmapId = "62847ab75c1b4f7d829a530b7c4432f2"; 
 

 
require(["dojo/dom", 
 

 
     "dojo/_base/array", 
 
     "dojo/parser", 
 
     "dojo/query", 
 
     "dojo/on", 
 

 
     "esri/Color", 
 
     "esri/config", 
 
     "esri/map", 
 
     "esri/arcgis/utils", 
 
     "esri/graphic", 
 

 
     "esri/geometry/normalizeUtils", 
 
     "esri/tasks/GeometryService", 
 
     "esri/tasks/BufferParameters", 
 
    
 
     "esri/toolbars/draw", 
 
    
 
     "esri/symbols/SimpleMarkerSymbol", 
 
     "esri/symbols/SimpleLineSymbol", 
 
     "esri/symbols/SimpleFillSymbol", 
 
     
 
     "dijit/layout/BorderContainer", 
 
     "dijit/layout/ContentPane", 
 
     "dijit/form/Button", "dojo/domReady!" 
 
     ], 
 
     function(dom, array, parser, query, on, Color, esriConfig, Map, arcgisUtils, Graphic, normalizeUtils, GeometryService, BufferParameters, Draw, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol){ 
 

 
     parser.parse(); 
 

 

 
     esriConfig.defaults.geometryService = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"); 
 

 
     esriConfig.defaults.io.proxyUrl = "/proxy/"; 
 
     esriConfig.defaults.io.alwaysUseProxy = false; 
 
    
 
     arcgisUtils.createMap(webmapId, "map").then(function (response) { 
 
     map = response.map; 
 
     }); 
 
     map.on("load", initToolbar); 
 

 
     //Setup button click handlers 
 
     on(dom.byId("clearGraphics"), "click", function(){ 
 
      if(map){ 
 
      map.graphics.clear(); 
 
      } 
 
     }); 
 
     //click handler for the draw tool buttons 
 
     query(".tool").on("click", function(evt){ 
 
      if(tb){ 
 
      tb.activate(evt.target.id); 
 
      } 
 
     }); 
 
     
 

 
     function initToolbar(evtObj) { 
 
     tb = new Draw(evtObj.map); 
 
     tb.on("draw-end", doBuffer); 
 
     } 
 

 
     function doBuffer(evtObj) { 
 
     tb.deactivate(); 
 
     var geometry = evtObj.geometry, symbol; 
 
     switch (geometry.type) { 
 
      case "point": 
 
      symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_SQUARE, 10, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255,0,0]), 1), new Color([0,255,0,0.25])); 
 
      break; 
 
      case "polyline": 
 
      symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASH, new Color([255,0,0]), 1); 
 
      break; 
 
      case "polygon": 
 
      symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_NONE, new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT, new Color([255,0,0]), 2), new Color([255,255,0,0.25])); 
 
      break; 
 
     } 
 

 
      var graphic = new Graphic(geometry, symbol); 
 
      map.graphics.add(graphic); 
 

 
      //setup the buffer parameters 
 
      var params = new BufferParameters(); 
 
      params.distances = [ dom.byId("distance").value ]; 
 
      params.outSpatialReference = map.spatialReference; 
 
      params.unit = GeometryService[dom.byId("unit").value]; 
 
      //normalize the geometry 
 

 
      normalizeUtils.normalizeCentralMeridian([geometry]).then(function(normalizedGeometries){ 
 
      var normalizedGeometry = normalizedGeometries[0]; 
 
      if (normalizedGeometry.type === "polygon") { 
 
       //if geometry is a polygon then simplify polygon. This will make the user drawn polygon topologically correct. 
 
       esriConfig.defaults.geometryService.simplify([normalizedGeometry], function(geometries) { 
 
       params.geometries = geometries; 
 
       esriConfig.defaults.geometryService.buffer(params, showBuffer); 
 
       }); 
 
      } else { 
 
       params.geometries = [normalizedGeometry]; 
 
       esriConfig.defaults.geometryService.buffer(params, showBuffer); 
 
      } 
 

 
      }); 
 
     } 
 

 
     function showBuffer(bufferedGeometries) { 
 
      var symbol = new SimpleFillSymbol(
 
      SimpleFillSymbol.STYLE_SOLID, 
 
      new SimpleLineSymbol(
 
       SimpleLineSymbol.STYLE_SOLID, 
 
       new Color([255,0,0,0.65]), 2 
 
      ), 
 
      new Color([255,0,0,0.35]) 
 
     ); 
 

 
      array.forEach(bufferedGeometries, function(geometry) { 
 
      var graphic = new Graphic(geometry, symbol); 
 
      map.graphics.add(graphic); 
 
      }); 
 

 
     } 
 
    }); 
 
</script> 
 

 
</head> 
 

 
<body class="claro"> 
 
<div data-dojo-type="dijit/layout/BorderContainer" 
 
    data-dojo-props="gutters:'true', design:'sidebar'" 
 
    style="width:100%;height:100%;"> 
 

 
    <div id="map" 
 
     data-dojo-type="dijit/layout/ContentPane" 
 
     data-dojo-props="region:'center'"> 
 
    </div> 
 

 
    <div id="leftPane" 
 
     data-dojo-type="dijit/layout/ContentPane" 
 
     data-dojo-props="region:'left'"> 
 
    <div class="details">Pick a tool and draw on the map. The drawn graphic will be buffered based on the specified parameters.</div> 
 
    <button type="button" class="tool" id="line">Line</button> 
 
    <button type="button" class="tool" id="polyline">Polyline</button> 
 
    <button type="button" class="tool" id="freehandpolyline">Freehand Polyline</button> 
 
    <br/> 
 
    <button type="button" class="tool" id="polygon">Polygon</button> 
 
    <button type="button" class="tool" id="freehandpolygon">Freehand Polygon</button> 
 
    <br/><hr /> 
 
    <div><b>Buffer Parameters</b></div> 
 
    Distance:&nbsp;<input type="text" id="distance" size="5" value="25" /> 
 
    <select id="unit" style="width:100px;"> 
 
     <option value="UNIT_STATUTE_MILE">Miles</option> 
 
     <option value="UNIT_FOOT">Feet</option> 
 
     <option value="UNIT_KILOMETER">Kilometers</option> 
 
     <option value="UNIT_METER">Meters</option> 
 
     <option value="UNIT_NAUTICAL_MILE">Nautical Miles</option> 
 
     <option value="UNIT_US_NAUTICAL_MILE">US Nautical Miles</option> 
 
     <option value="UNIT_DEGREE">Degrees</option> 
 
    </select><br /> 
 
    <button type="button" id="clearGraphics" type="button">Clear Graphics</button> 
 
    </div> 
 
</div> 
 
</body> 
 
</html>

+0

'沒有工作' ......請解釋一下。 –

回答

0

您正在使用webmap和您所附加在其上「加載」事件,這是不需要的。下面是詳細的解釋:

  1. 您的地圖目標,因爲你是「上」地圖的方法調用webmap的到來響應之前不確定的。 (這不是必需的)

  2. 如果你得到「response.map」,這意味着你的地圖對象被加載。所以需要附加「加載」事件

  3. 您的地圖對象已經加載,所以只需要初始化您的繪圖工具欄。

下面是工作代碼:

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
 
<!--The viewport meta tag is used to improve the presentation and behavior of the samples 
 
    on iOS devices--> 
 
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"> 
 
<title>Buffer</title> 
 

 
<link rel="stylesheet" href="http://js.arcgis.com/3.13/dijit/themes/claro/claro.css"> 
 
<link rel="stylesheet" href="http://js.arcgis.com/3.13/esri/css/esri.css"> 
 
<style> 
 
    html, body { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
    overflow:hidden; 
 
    } 
 
    #leftPane{ 
 
    color:#000; 
 
    width:250px; 
 
    padding-bottom:15px; 
 
    } 
 
    #map{ 
 
    padding:0; 
 
    } 
 
    .details{ 
 
    font-size:14px; 
 
    font-weight:600; 
 
    padding-bottom:20px; 
 
    } 
 

 
    button{ 
 
    margin:2px; 
 
    cursor:pointer; 
 
    } 
 
</style> 
 

 
<script src="http://js.arcgis.com/3.13/"></script> 
 
<script> 
 
var map, tb, 
 
     webmapId = "62847ab75c1b4f7d829a530b7c4432f2"; 
 

 
require(["dojo/dom", 
 

 
     "dojo/_base/array", 
 
     "dojo/parser", 
 
     "dojo/query", 
 
     "dojo/on", 
 

 
     "esri/Color", 
 
     "esri/config", 
 
     "esri/map", 
 
     "esri/arcgis/utils", 
 
     "esri/graphic", 
 

 
     "esri/geometry/normalizeUtils", 
 
     "esri/tasks/GeometryService", 
 
     "esri/tasks/BufferParameters", 
 
    
 
     "esri/toolbars/draw", 
 
    
 
     "esri/symbols/SimpleMarkerSymbol", 
 
     "esri/symbols/SimpleLineSymbol", 
 
     "esri/symbols/SimpleFillSymbol", 
 
     
 
     "dijit/layout/BorderContainer", 
 
     "dijit/layout/ContentPane", 
 
     "dijit/form/Button", "dojo/domReady!" 
 
     ], 
 
     function(dom, array, parser, query, on, Color, esriConfig, Map, arcgisUtils, Graphic, normalizeUtils, GeometryService, BufferParameters, Draw, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol){ 
 

 
     parser.parse(); 
 

 

 
     esriConfig.defaults.geometryService = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"); 
 

 
     esriConfig.defaults.io.proxyUrl = "/proxy/"; 
 
     esriConfig.defaults.io.alwaysUseProxy = false; 
 
    
 
     arcgisUtils.createMap(webmapId, "map").then(function (response) { 
 
     map = response.map; 
 
     initToolbar(map); 
 
     }); 
 
     
 
     //Setup button click handlers 
 
     on(dom.byId("clearGraphics"), "click", function(){ 
 
      if(map){ 
 
      map.graphics.clear(); 
 
      } 
 
     }); 
 
     //click handler for the draw tool buttons 
 
     query(".tool").on("click", function(evt){ 
 
      if(tb){ 
 
      tb.activate(evt.target.id); 
 
      } 
 
     }); 
 
     
 

 
     function initToolbar(mapObj) { 
 
     tb = new Draw(mapObj); 
 
     tb.on("draw-end", doBuffer); 
 
     } 
 

 
     function doBuffer(evtObj) { 
 
     tb.deactivate(); 
 
     var geometry = evtObj.geometry, symbol; 
 
     switch (geometry.type) { 
 
      case "point": 
 
      symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_SQUARE, 10, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255,0,0]), 1), new Color([0,255,0,0.25])); 
 
      break; 
 
      case "polyline": 
 
      symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASH, new Color([255,0,0]), 1); 
 
      break; 
 
      case "polygon": 
 
      symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_NONE, new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASHDOT, new Color([255,0,0]), 2), new Color([255,255,0,0.25])); 
 
      break; 
 
     } 
 

 
      var graphic = new Graphic(geometry, symbol); 
 
      map.graphics.add(graphic); 
 

 
      //setup the buffer parameters 
 
      var params = new BufferParameters(); 
 
      params.distances = [ dom.byId("distance").value ]; 
 
      params.outSpatialReference = map.spatialReference; 
 
      params.unit = GeometryService[dom.byId("unit").value]; 
 
      //normalize the geometry 
 

 
      normalizeUtils.normalizeCentralMeridian([geometry]).then(function(normalizedGeometries){ 
 
      var normalizedGeometry = normalizedGeometries[0]; 
 
      if (normalizedGeometry.type === "polygon") { 
 
       //if geometry is a polygon then simplify polygon. This will make the user drawn polygon topologically correct. 
 
       esriConfig.defaults.geometryService.simplify([normalizedGeometry], function(geometries) { 
 
       params.geometries = geometries; 
 
       esriConfig.defaults.geometryService.buffer(params, showBuffer); 
 
       }); 
 
      } else { 
 
       params.geometries = [normalizedGeometry]; 
 
       esriConfig.defaults.geometryService.buffer(params, showBuffer); 
 
      } 
 

 
      }); 
 
     } 
 

 
     function showBuffer(bufferedGeometries) { 
 
      var symbol = new SimpleFillSymbol(
 
      SimpleFillSymbol.STYLE_SOLID, 
 
      new SimpleLineSymbol(
 
       SimpleLineSymbol.STYLE_SOLID, 
 
       new Color([255,0,0,0.65]), 2 
 
      ), 
 
      new Color([255,0,0,0.35]) 
 
     ); 
 

 
      array.forEach(bufferedGeometries, function(geometry) { 
 
      var graphic = new Graphic(geometry, symbol); 
 
      map.graphics.add(graphic); 
 
      }); 
 

 
     } 
 
    }); 
 
</script> 
 

 
</head> 
 

 
<body class="claro"> 
 
<div data-dojo-type="dijit/layout/BorderContainer" 
 
    data-dojo-props="gutters:'true', design:'sidebar'" 
 
    style="width:100%;height:100%;"> 
 

 
    <div id="map" 
 
     data-dojo-type="dijit/layout/ContentPane" 
 
     data-dojo-props="region:'center'"> 
 
    </div> 
 

 
    <div id="leftPane" 
 
     data-dojo-type="dijit/layout/ContentPane" 
 
     data-dojo-props="region:'left'"> 
 
    <div class="details">Pick a tool and draw on the map. The drawn graphic will be buffered based on the specified parameters.</div> 
 
    <button type="button" class="tool" id="line">Line</button> 
 
    <button type="button" class="tool" id="polyline">Polyline</button> 
 
    <button type="button" class="tool" id="freehandpolyline">Freehand Polyline</button> 
 
    <br/> 
 
    <button type="button" class="tool" id="polygon">Polygon</button> 
 
    <button type="button" class="tool" id="freehandpolygon">Freehand Polygon</button> 
 
    <br/><hr /> 
 
    <div><b>Buffer Parameters</b></div> 
 
    Distance:&nbsp;<input type="text" id="distance" size="5" value="25" /> 
 
    <select id="unit" style="width:100px;"> 
 
     <option value="UNIT_STATUTE_MILE">Miles</option> 
 
     <option value="UNIT_FOOT">Feet</option> 
 
     <option value="UNIT_KILOMETER">Kilometers</option> 
 
     <option value="UNIT_METER">Meters</option> 
 
     <option value="UNIT_NAUTICAL_MILE">Nautical Miles</option> 
 
     <option value="UNIT_US_NAUTICAL_MILE">US Nautical Miles</option> 
 
     <option value="UNIT_DEGREE">Degrees</option> 
 
    </select><br /> 
 
    <button type="button" id="clearGraphics" type="button">Clear Graphics</button> 
 
    </div> 
 
</div> 
 
</body> 
 
</html>

+0

:)歡迎Hussien先生.. –

相關問題