按道理来说,extent与execute应该不会相互影响。
今天就碰上了一个奇怪的问题,如果把extent注释掉,execute就不好用了……
源代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Resizable Map</title>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.0/js/dojo/dijit/themes/tundra/tundra.css"/>
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis?v=2.0"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js"></script>
<script>
//地图div的宽高
var wid;
var hei;
var map;
var graphicLayer;
dojo.require("esri.map");
dojo.require("esri.toolbars.navigation");
dojo.require("esri.tasks.query");
function init(){
var initExtent = new esri.geometry.Extent({"xmin": 76252, "ymin": 295635, "xmax": 135783, "ymax": 305322, "spatialReference": { "wkid": 106000 } });
map = new esri.Map("map",{//加载地图
logo:false,//不显示logo
extent: initExtent
});
var myTiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://192.168.0.181/ArcGIS/rest/services/GISBaseMap/MapServer");
map.addLayer(myTiledMapServiceLayer);
var myTiledMapServiceLayer1 = new esri.layers.ArcGISTiledMapServiceLayer("http://192.168.0.181/ArcGIS/rest/services/ModelPipe_BaseMap/MapServer");
map.addLayer(myTiledMapServiceLayer1);
}
window.οnlοad=function(){
/*事件监听,div宽高变化*/
/*首先获取div当前宽高,然后每隔一秒钟获取一次div当前宽高,然后同上一次获取到的宽高进行比较,如果改变,则触发事件,重新加载地图*/
wid=$('#map').width();
hei=$('#map').height();
setInterval("changed()",100);
}
//监听地图宽高改变事件
function changed(){
var wid1=$('#map').width();
var hei1=$('#map').height();
if(wid1!=wid || hei1!=hei){
wid=wid1;
hei=hei1;
window.map.resize();
}
}
//添加点
function mapGraphicAdd(x,y){
if(graphicLayer!=null){
graphicLayer.clear();
}
graphicLayer = new esri.layers.GraphicsLayer();
map.addLayer(graphicLayer);
var point = new esri.geometry.Point(x,y);
var infoTemplate = new esri.InfoTemplate("point","<div style='background-color:red;border:1px solid red'>this is the new point</div>");
var simpleMarkerSymbol = new esri.symbol.SimpleMarkerSymbol();
var graphic = new esri.Graphic(point,simpleMarkerSymbol);
graphicLayer.add(graphic.setInfoTemplate(infoTemplate));
}
//清除点
function mapClear(){
graphicLayer.clear();
}
dojo.addOnLoad(init);
//获取阀门详细信息
var fs;
function getfamen(ii) {
var cityQueryTask = new esri.tasks.QueryTask("http://192.168.0.181/ArcGIS/rest/services/ModelPipe_BaseMap/MapServer/3");
var cityQuery = new esri.tasks.Query();
cityQuery.outFields = ["*"];
cityQuery.returnGeometry = true;
cityQuery.outSpatialReference = map.spatialReference;
cityQuery.where = "ElementId =" + ii;
cityQueryTask.execute(cityQuery, addCityFeatureSetToMap);
}
function addCityFeatureSetToMap(featureSet) {
var symbol = new esri.symbol.SimpleMarkerSymbol();
symbol.setColor(new dojo.Color([0, 0, 255]));
var infoTemplate = new esri.InfoTemplate("阀门信息", "${*}");
fs = featureSet;
dojo.forEach(featureSet.features, function (feature) {
graphicLayer.add(feature.setSymbol(symbol).setInfoTemplate(infoTemplate));
});
}
</script>
</head>
<body>
<div id="map" style="width: 50%;height: 50%;border: 1px solid red"></div>
<div οnclick="mapGraphicAdd(95353.359,304542.613)">添加</div>
<div οnclick="getfamen('275139')">添加</div>
<div οnclick="mapClear()">清除</div>
</body>
</html>
这里,首先点击第一个添加,向地图上加入一个x:95353.359,y:304542.613的标记,然后点击第二个添加,从
http://192.168.0.181/ArcGIS/rest/services/ModelPipe_BaseMap/MapServer/3 图层中获取第二个点ElementId=275139的坐标,添加到地图上。
问题是,如果我将代码中 extent: initExtent 这一行注释掉的话,点击第二个添加就不会有第二个点添加到地图上了。代码会运行到
cityQueryTask.execute(cityQuery, addCityFeatureSetToMap); 这一行。
设置extent 的目的是为了初始化地图在首次加载时候的位置,而 execute()是arcgis的图层要素查询的功能。这两个按道理来讲是没有太大联系的。难道是因为没有设置wkid,导致graphicLayer找不到点吗?