jquery Deferred
// 创建地址解析器实例 var myGeo = new BMap.Geocoder(); // 将地址解析结果显示在地图上, 需要2个地址 用Deferred控制 var point1, point2; // 起点 和终点 var dtd1 = $.Deferred(); var dtd2 = $.Deferred(); if (options.start && options.end) { //蒙板加载中 $("#" + options.target).RouMask();
//调用 addParsePoint 方法 传入Deferred对象 当对个Deferred对象状态都为resolve时, $.when的done方法才会执行
$.when(addParsePoint(dtd1, options.start, "start"), addParsePoint(dtd2, options.end, "end")).done(function () { var driving = new BMap.DrivingRoute(map, { renderOptions: { map: map, autoViewport: true } }); driving.search(point1, point2); //关闭蒙板 $("#" + options.target).RouMask("close"); }) }; //获取坐标 function addParsePoint(dtd, addName, t) { myGeo.getPoint(addName, function (point) { if (point) { if (t == "end") { point2 = point; } else { point1 = point; } //完成 dtd.resolve(); //map.centerAndZoom(point, 16); //map.addOverlay(new BMap.Marker(point)); } }, "全国"); return dtd; }// end addParsePoint