This sample shows how to use esri.arcgis.utils.createWebMap to create a map using an object that is a by value
representation of the webmap.This is an alternative to other samples in this folder that create a map using the webmap id from ArcGIS.com. The code below creates a new webmap and sets the title, description and initial extent of the map.
var webmap = {};
webmap.item = {
"title":"Soil Survey Map of USA",
"snippet": "Detailed description of data",
"extent": [[-139.4916, 10.7191],[-52.392, 59.5199]]
};
Next, the layers that make up the map are defined. In this sample, the World Terrain basemap from ArcGIS.com is added along with an overlay layer that adds additional information to the map such as boundaries, cities, water features and landmarks and roads. An operational layer is added that displays soil information from the U.S. Department of Agriculture.
webmap.itemData = {
"operationalLayers": [{
"url": "http://server.arcgisonline.com/ArcGIS/rest/services/Specialty/Soil_Survey_Map/MapServer",
"visibility": true,
"opacity": 0.75,
"title": "Soil Survey Map",
"itemId": "204d94c9b1374de9a21574c9efa31164"
}],
"baseMap": {
"baseMapLayers": [{
"opacity": 1,
"visibility": true,
"url": "http://services.arcgisonline.com/ArcGIS/rest/services/World_Terrain_Base/MapServer"
},{
"isReference": true,
"opacity": 1,
"visibility": true,
"url": "http://services.arcgisonline.com/ArcGIS/rest/services/Reference/World_Reference_Overlay/MapServer"
}],
"title": "World_Terrain_Base"
},
"version": "1.1"
};
Note: You can retrieve the item definition from an ArcGIS.com item using esri.arcgis.utils.getItem()
Once the webmap is created, we'll use the createMap method to build a map from the provided information.
var mapDeferred = esri.arcgis.utils.createMap(webmap, "map", {
mapOptions: {
slider: true
},
geometryServiceURL: "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />
<title>
Create web map from JSON
</title>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.7/js/dojo/dijit/themes/claro/claro.css">
<style type="text/css">
html,body {
height:100%;
width:100%;
margin:0;
padding:0;
}
body {
background-color:#FFF;
overflow:hidden;
font-family:"Helvetica";
}
#header {
border:solid 1px #A8A8A8;
overflow:hidden;
background-color:#999;
background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#C0C0C0));
background: -moz-linear-gradient(top, #fff, #C0C0C0);
height:65px;
margin: 5px 5px;
}
.roundedCorners {
-o-border-radius:4px;
-moz-border-radius:4px;
-webkit-border-radius:4px;
border-radius:4px;
}
.shadow {
box-shadow:4px 4px 8px #adadad;
-webkit-box-shadow:4px 4px 8px #adadad;
-moz-box-shadow:4px 4px 8px #adadad;
-o-box-shadow:4px 4px 8px #adadad;
}
#title{
padding-top:2px;
padding-left:10px;
color:#000;
font-size:18pt;
text-align:left;
text-shadow: 0px 1px 0px #e5e5ee;
font-weight:700;
}
#subtitle {
font-size:small;
padding-left:40px;
text-shadow: 0px 1px 0px #e5e5ee;
color:#000;
}
#rightPane{
background-color:#E8E8E8;
border:solid 2px #B8B8B8;
margin:5px;
width:20%;
}
#map {
background-color:#FFF;
border:solid 2px #B8B8B8;
margin:5px;
padding:0;
}
</style>
<script type="text/javascript">
var dojoConfig = {
parseOnLoad: true
};
</script>
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.7">
</script>
<script type="text/javascript">
dojo.require("dijit.dijit");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("esri.map");
dojo.require("esri.arcgis.utils");
dojo.require("esri.dijit.Legend");
dojo.require("esri.dijit.Scalebar");
var map;
function init() {
var webmap = {};
webmap.item = {
"title":"Soil Survey Map of USA",
"snippet": "This map shows the Soil Survey Geographic (SSURGO) by the United States Department of Agriculture's Natural Resources Conservation Service.",
"extent": [[-139.4916, 10.7191],[-52.392, 59.5199]]
};
webmap.itemData = {
"operationalLayers": [{
"url": "http://server.arcgisonline.com/ArcGIS/rest/services/Specialty/Soil_Survey_Map/MapServer",
"visibility": true,
"opacity": 0.75,
"title": "Soil Survey Map",
"itemId": "204d94c9b1374de9a21574c9efa31164"
}],
"baseMap": {
"baseMapLayers": [{
"opacity": 1,
"visibility": true,
"url": "http://services.arcgisonline.com/ArcGIS/rest/services/World_Terrain_Base/MapServer"
},{
"isReference": true,
"opacity": 1,
"visibility": true,
"url": "http://services.arcgisonline.com/ArcGIS/rest/services/Reference/World_Reference_Overlay/MapServer"
}],
"title": "World_Terrain_Base"
},
"version": "1.1"
};
dojo.byId("title").innerHTML = webmap.item.title;
dojo.byId("subtitle").innerHTML = webmap.item.snippet;
var mapDeferred = esri.arcgis.utils.createMap(webmap, "map", {
mapOptions: {
slider: true
},
geometryServiceURL: "http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"
});
mapDeferred.addCallback(function(response) {
map = response.map;
var initExtent = new esri.geometry.Extent({"xmin":-13529153.240034927,"ymin":4665612.430933277,"xmax":-13281497.268391073,"ymax":4756113.872422882,"spatialReference":{"wkid":102100}});
map.setExtent(initExtent);
var layers = response.itemInfo.itemData.operationalLayers;
if(map.loaded){
initMap(layers);
}
else{
dojo.connect(map,"onLoad",function(){
initMap(layers);
});
}
});
mapDeferred.addErrback(function(error) {
console.log("Map creation failed: ", dojo.toJson(error));
});
}
function initMap(layers){
//add a scalebar
var scalebar = new esri.dijit.Scalebar({
map:map,
scalebarUnit: 'english'
});
//add a legend
var layerInfo = dojo.map(layers, function(layer,index){
return {layer:layer.layerObject,title:layer.title};
});
if(layerInfo.length > 0){
var legendDijit = new esri.dijit.Legend({
map:map,
layerInfos:layerInfo
},"legend");
legendDijit.startup();
}
else{
dojo.byId('legend').innerHTML = 'No operational layers';
}
//resize the map when the browser resizes
dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
}
//show map on load
dojo.addOnLoad(init);
</script>
</head>
<body class="claro">
<div id="mainWindow" data-dojo-type="dijit.layout.BorderContainer" data-dojo-props="design:'headline'" style="width:100%; height:100%;">
<div id="header" class="shadow roundedCorners" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'top'">
<div id="title"></div>
<div id="subtitle" > </div>
</div>
<div id="map" class="roundedCorners shadow" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'center'"></div>
<div id="rightPane" class="roundedCorners shadow" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'right'" >
<div id="legend"></div>
</div>
</div>
</body>
</html>