需求如下,在点击按钮时,根据相关按钮ajax 取得对应的 地图地址,大概想搞成的类似于这样的效果。
function initMap()
{
$.post("/myMap/GetMapUrl",
{ Param:txtName},
function (data, status) {
data = JSON.parse(data);
var view;
var map;
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/MapImageLayer",
"esri/widgets/LayerList",
"esri/widgets/Search",
"dojo/domReady!"
], function (Map, MapView, MapImageLayer, LayerList, Search) {
map = new Map("mapDiv");
var layer = new MapImageLayer({
url: Data //根据ajax 取得的地址
})
map.add(layer);
view = new MapView({
container: "mapDiv", // Reference to the DOM node that will contain the view
map: map // References the map object created in step 3
});
});
}
}
然后其他地方 的Btn 点击的时候调用,但现在存在 如果这么搞 会死循环导致网页崩溃卡死,如果
require(["esri/layers/MapImageLayer"]);
require(["esri/layers/LayerList"]);
require(["esri/Map"]);
require(["esri/views/MapView"]); 这样申请全局变量 在其他 function 中 还是找不到 map.addLayer(layer2); 的方法 提示 未定义等