0

我可以得到mapUrl 使用函数fecthData。但它无法显示地图。如果我评论了警报行代码,它将显示地图。任何人都可以给我一些提示吗?提前致谢。

dojo.require("esri.map");
var mapLayer;
var mapUrl;
function init() {
   // use ajax to get map url
   fecthData("MapInfo?id=1", function (d) {mapUrl = d.mapUrl;});

   map = new esri.Map("map");
   // if um-commented below line, the map will show
   //alert("mapUrl"+mapUrl);

   // map layer
   mapLayer = new esri.layers.ArcGISDynamicMapServiceLayer(mapUrl);
   if ( mapLayer == undefined )
       return;
   dojo.connect(mapLayer, "onLoad", initFunc);
   map.addLayer(mapLayer);
}
function fecthData(varUrl, varFunc) {
   var options = {
    type: "POST",
    url: varUrl,
    data: "",
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    cache: false,
    success: varFunc
 };
  //execute the ajax call and get a response
  var request = $.ajax(options);
}
dojo.addOnLoad(init);
4

2 回答 2

0

看起来您的“fecthData”在“init”执行结束后得到响应。我想您应该在“fecthData”完成后立即初始化您的地图,如下所示:

dojo.require("esri.map");
var map, mapUrl, mapLayer;

function init() {
   fecthData("MapInfo?id=1");
}
function fecthData(varUrl) {
   var options = {
    type: "POST",
    url: varUrl,
    data: "",
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    cache: false,
    success: function (d) {
        mapUrl = d.mapUrl;
        if ( mapUrl == undefined )
            return;
        map = new esri.Map("map");
        mapLayer = new esri.layers.ArcGISDynamicMapServiceLayer(mapUrl);
        dojo.connect(map, "onLoad", someFunction); // layer object doesn't have any onLoad event, so I changed it
        map.addLayer(mapLayer);
    }
 };
 var request = $.ajax(options);
}
dojo.addOnLoad(init);

或类似的东西

于 2012-12-06T16:05:08.997 回答
0

现在好了。

dojo.require("esri.map");
var mapLayer;
var mapUrl;
function init() {
  fecthData("MapInfo?id=1",getMapInfo);
}

 function getMapInfo(data) {
    mapUrl= data.mapUrl;
    if ( mapUrl== undefined )
        return;
    map = new esri.Map("map");
    mapLayer = new esri.layers.ArcGISDynamicMapServiceLayer(mapUrl);
    if ( mapLayer == undefined )
       return;
    map.addLayer(mapLayer);
    dojo.connect(mapLayer, "onLoad", initFunc);
}
function fecthData(varUrl, varFunc) {
var options = {
    type: "POST",
    url: varUrl,
    data: "",
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    cache: false,
    success: varFunc
};
//execute the ajax call and get a response
var request = $.ajax(options);
}
dojo.addOnLoad(init);
于 2012-12-10T05:54:15.940 回答