描述

本例展示了如何设置地图的范围为地图其中一个图层的范围。本例有两个图层:ArcGIS Online上的世界地图图层ArcGISTiledMapServiceLayer和堪萨斯州的要素的图层ArcGISDynamicMapServiceLayer。本例展示了如何设置地图总是以堪萨斯州范围开始。

代码包含两个事件监听器,一个是为了每个图层。这些监听器帮助记录多少图层被加载。当图层计数是2,createMapAddLayers函数被调用。这个函数创建一个地图,设置地图范围是myService2(堪萨斯州服务)的范围:

myMap = new esri.Map("mapDiv", { extent:myService2.fullExtent });

地图被创建以后,图层被增加。注意加载图层和增加图层不是一回事。在本例中,地图被创建前图层加载,地图创建以后图层被加到地图里。

直到所有图层被加载前要避免访问地图属性。如果代码中没有包含事件监听器,有可能在myService2完全加载前地图就会尝试去设置它的范围,这回引起意想不到的结果。

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>Set Map Extent Using Second Service</title> <link rel="stylesheet" type="text/css" href="styles.css"
href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css">
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script>
<script type="text/javascript">
dojo.require("esri.map");
var map,myservice1,myservice2; function init(){
map=new esri.Map("map");
myService1=new esri.layers.ArcGISTiledMapServiceLayer(
"http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"
// "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer"
); var secondaryMapServiceURL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Petroleum/KGS_OilGasFields_Kansas/MapServer";
//var secondaryMapServiceURL = "http://118.144.36.6:6080/arcgis/rest/services/chakan/henan530/MapServer";
myService2 = new esri.layers.ArcGISDynamicMapServiceLayer(secondaryMapServiceURL,{opacity:0.65}); var layerLoadCount=0;
//当两个图层加载时运行creatMapAddLayers if(myService1.loded){
layerLoadCount+=1;
if(layerLoadCount == 2){
creatMapAddLayers(myService1,myService2);
}
}else{
dojo.connect(myservice1,"onLoad",function(service){
layerLoadCount += 1;
if(layerLoadCount == 2){
creatMapAddLayers(myService1,myService2);
}
});
}
if(myService2.loded){
layerLoadCount+=1;
if(layerLoadCount == 2){
creatMapAddLayers(myService1,myService2);
} }else{
dojo.connect(myservice2,"onLoad",function(service){
layerLoadCount += 1;
if(layerLoadCount == 2){
creatMapAddLayers(myService1,myService2);
} });
}
//创建一个地图,设置范围并添加到地图服务中
function creatMapAddLayers(myService1,myService2){
//创建地图
map=new esri.Map("mapDiv",{extent:myservice2.fullExtent});
map.addLayer(myService1);
map.addLayer(myService2);
dojo.connect(map,"onClick",addPoint);
function addPoint(event){
map.infoWindow.setTitle("Coordinates-坐标");
map.infoWindow.setContent("经/纬:" + event.mapPoint.x + "," + event.mapPoint.y
+"<br/>screen x/y: " + event.screenPoint.x + ","+event.screenPoint.y); map.infoWindow.show(event.screenPoint,map.getInfoWindowAnchor(event.screenPoint));
} } }
dojo.addOnLoad(init); </script>
</head> <body class="tundra">
<div id="map" style="width:600px;height:400px;border:1px solid #000"></div>
<br>
This map shows two services:
<ul>
<li>An ArcGIS Online tiled service that has a world extent.</li>
<li>A second dynamic service with an extent of the State of Kansas. This is the extent used when the maps are first displayed. </li>
</ul> Note that if you want to combine to tiled services in the same map, they must have the same tile configuration.
</body>
</html>
05-28 19:18