百度地圖中坐標(biāo)轉(zhuǎn)換的JavaScript API示例官方示例如下:
var points = [new BMap.Point(116.3786889372559,39.90762965106183), new BMap.Point(116.38632786853032,39.90795884517671), new BMap.Point(116.39534009082035,39.907432133833574), new BMap.Point(116.40624058825688,39.90789300648029), new BMap.Point(116.41413701159672,39.90795884517671) ];//地圖初始化var bm = new BMap.Map("allmap"); bm.centerAndZoom(new BMap.Point(116.378688937,39.9076296510), 15);//坐標(biāo)轉(zhuǎn)換完之后的回調(diào)函數(shù)translateCallback = function (data){ if(data.status === 0) {for (var i = 0; i < data.points.length; i++) { bm.addOverlay(new BMap.Marker(data.points[i])); bm.setCenter(data.points[i]); } } } setTimeout(function(){var convertor = new BMap.Convertor(); convertor.translate(points, 1, 5, translateCallback) }, 1000);
改寫后:
將上述代碼改寫為傳統(tǒng)的JavaScript回調(diào)函數(shù)形式:
function translate(points,callback){ var convertor=new BMap.Convertor(); convertor.translate(points, 1, 5, callback) translateCallback = function (data){if(data.status === 0) { var res=[]; //res用來存放轉(zhuǎn)換后的points數(shù)組 for (var i = 0; i < data.points.length; i++) { bm.addOverlay(new BMap.Marker(data.points[i])); res.push(data.points[i]); } callback(res) } } }//再調(diào)用translate函數(shù),translate(points,function(data){ console.log(data); //data為轉(zhuǎn)換后的points數(shù)組})
改寫后translate函數(shù)接受兩個參數(shù),第一個參數(shù)為原始的百度points數(shù)組,第二個參數(shù)傳入一個回調(diào)函數(shù)。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com