使用的百度地圖。必須先使用百度地圖獲取目標(biāo)位置和使用百度地圖獲取目標(biāo)位置,然后在通過百度地圖的獲取我和目標(biāo)的位置。由于異步的原因,所以我使用了定時器解決,不過性能肯定不好,如何使用async await或者promise代替定時器呢?代碼片段如下: // 獲取目標(biāo)位置 var myGeo = new BMap.Geocoder(); // 根據(jù)中地址名 查經(jīng)緯度 myGeo.getPoint(scope.targetname, function(point){ if (point) { scope.target = point; }else{ toast("您選擇地址沒有解析到結(jié)果!"); } }, scope.cityname);// 獲取我的位置 var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ // alert('您的位置:'+r.point.lng+','+r.point.lat); scope.myadd = new BMap.Point(r.point.lng,r.point.lat); // console.log(scope.myadd,"我的位置") } else { alert("定位失敗"); } },{enableHighAccuracy: true}) // 獲取我和目標(biāo)的位置 setTimeout(function(){ if(scope.myadd!=""&&scope.target!=""){ var map = new BMap.Map("l-map"); map.centerAndZoom(new BMap.Point(scope.target.lng,scope.target.lat), 11); var walking = new BMap.WalkingRoute(map, {renderOptions: {map: map, panel: "r-result", autoViewport: true}}); walking.search(scope.myadd, scope.target); } }, 1000);
如何使用async await或者promise代替定時器呢?
慕森王
2019-03-12 15:19:15