Before Rewriting:
The official example of the JavaScript API example for coordinate conversion in Baidu maps is as Follows:
varpoints = [NewBmap.point (116.3786889372559,39.90762965106183), NewBmap.point (116.38632786853032,39.90795884517671), NewBmap.point (116.39534009082035,39.907432133833574), NewBmap.point (116.40624058825688,39.90789300648029), NewBmap.point (116.41413701159672,39.90795884517671)];//Map InitializationvarBM =NewBmap.map ("allmap"); Bm.centerandzoom (NewBmap.point (116.378688937,39.9076296510), 15);//callback function after the coordinate conversion is completeTranslatecallback =function(data) {if(data.status = = 0) { for(vari = 0; I < data.points.length; i++) {bm.addoverlay (NewBmap.marker (data.points[i])); Bm.setcenter (data.points[i]); }}}settimeout (function(){ varConvertor =NewBmap.convertor (); Convertor.translate (points,1, 5, translatecallback)},1000);
After Rewriting:
Rewrite the above code as a traditional JavaScript callback function:
functionTranslate (points,callback) {varConvertor=NewBmap.convertor (); Convertor.translate (points,1, 5, Callback) translatecallback=function(data) {if(data.status = = 0) { varres=[];//Res is used to store the converted points array for(vari = 0; I < data.points.length; i++) {bm.addoverlay (NewBmap.marker (data.points[i])); Res.push (data.points[i]); } Callback (res) }}}//call the Translate function again,Translate (points,function(data) {console.log (data);//data is the converted points array})
After rewriting the Translate function accepts two parameters, the first parameter is the original Baidu points array, the second parameter passed in a callback Function.
Rewrite the official JavaScript API example of Baidu coordinate conversion to the traditional form of callback function