官术网_书友最值得收藏!

Loading API modules

Before you can create a Map object you must first reference the module that provides the map functionality. This is accomplished through the use of the require() function.

Whether to use the older legacy style of Dojo or the new AMD is a source of frustration for many developers. AMD, or Asynchronous Model Definition, was introduced at version 1.7 of Dojo. The 3.4 release of the ArcGIS Server API for JavaScript was the first version to have all modules re-written. For the time being both legacy and AMD style will work just fine, but we advise you to write all your new applications using the AMD style. This is becoming increasingly important as the drop date for Dojo 2.0 gets nearer, because this new version will no longer support the legacy style. We'll follow that convention in this book but keep in mind that applications written prior to version 3.4 of the API still reflect the older style of coding.

Before you begin adding code to the Sandbox, please remove the following code you see listed. To ensure that you remove the correct code, look for the <script> tag that contains the call to the require() function. You need to remove everything between the opening <script> and closing </script> tags. You will write the JavaScript code to create the map yourself:

<script>
var map;

require(["esri/map", "dojo/domReady!"], function(Map) {
map = new Map("map", {
basemap: "topo", //For full list of pre-defined basemaps,
navigate to http://arcg.is/1JVo6Wd
center: [-122.45, 37.75], // longitude, latitude
zoom: 13
});
});
</script>

The require() function is used to import resources into your web page. Various resources are provided by the ArcGIS API for JavaScript including the esri/map resource, which must be referenced before you can create a map or work with geometry, graphics, and symbols. Once you have a reference to esri/map you can use the Map constructor to create the map.

The modules you want to import must be contained within a new <script> tag. Add the following lines of code to the Sandbox inside the <head> tag:

<script>
require([], function() {

});
</script>

The require() function accepts an array of module names, followed by a (usually anonymous) callback function that will be executed when the resources that the modules represent are loaded and are available to the application.

The callback function's parameters are the alias names you will use to refer to the modules in your code. Therefore the first argument to the function will refer to the first module element in the array, the second argument will refer to the second module element in the array, and so on.

The argument names used inside the require() function's callback can be named anything you'd like. However, both ESRI and Dojo provide a list of preferred argument names and we recommend adhering to those lists to make it easier for others to understand your code.

The ArcGIS API for JavaScript preferred aliases can be found here: https://developers.arcgis.com/javascript/3/jsapi/argument_aliases.html and the Dojo preferred aliases can be found in a Google Spreadsheet here: http://preview.tinyurl.com/y8ajhy7y.

For example, in the following code that you add, we provide a reference to the esri/map resource, and then inside the anonymous function we provide a preferred argument of Map. Each module that you reference in the require() function will have an associated argument which will provide your code with access to that module, with one main exception, which we will cover as follows:

<script>     
    require(["esri/map"], function(Map) { 
         
    });  
</script> 
主站蜘蛛池模板: 凤凰县| 成都市| 宜春市| 昌乐县| 磐安县| 惠安县| 永兴县| 汶川县| 张掖市| 扎赉特旗| 承德市| 抚顺市| 盘锦市| 冀州市| 宽城| 博乐市| 宜良县| 东源县| 许昌县| 突泉县| 屏东县| 萨嘎县| 龙州县| 庆安县| 金塔县| 东港市| 易门县| 巴东县| 白城市| 筠连县| 潜山县| 三原县| 沛县| 都江堰市| 南和县| 贵溪市| 锦屏县| 繁昌县| 来宾市| 昂仁县| 鲁山县|