ArcGIS Javascript API - 如何使用jquery ajax获取地图网址

时间:2012-11-15 06:22:45

标签: javascript arcgis

我可以使用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);

2 个答案:

答案 0 :(得分: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);

或类似的东西

答案 1 :(得分: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);