最近做自己的网站,调用百度地图的js API,在页面上显示一些坐标点。遇到一个问题,就是当坐标点超过10个时,convertor.translate(tempPointArr, 1, 5, translateCallback)执行完后,在translateCallback中数据的状态不是0,应该就是GPS经纬度转换没有完成,导致坐标点不会被添加到地图上显示。不知道原因所在,看API文档应该能找到答案,无奈装的艾普宽带,现在百度地图的js API网页打不开。把坐标转换分成10个一组来做,可以解决这个问题。
var groups = 0; //坐标转换完之后的回调函数 translateCallback = function (data){ if(data.status === 0) { for (var i = 0; i < data.points.length; i++) { var marker = new BMap.Marker(data.points[i]); var label = new BMap.Label(groups * 10 + i,{offset:new BMap.Size(20,-10)}); marker.setLabel(label); //添加百度label map.addOverlay(marker); } groups++; map.setCenter(data.points[i-1]); //alert("the center is the: " + (i - 1) + "th point"); } else { alert("data status is not 0"); } } setTimeout(function(){ var convertor = new BMap.Convertor(); var n = 10; alert("the totall number of points is : " + pointArr.length); var remain = pointArr.length % 10; var rounds = (pointArr.length - remain) / 10; alert(rounds + " groups of points and " + remain + " points to translate"); var tempPointArr = []; var index = 0; for(var r = 0; r < rounds; r++) { tempPointArr = []; for(var i = 0; i < n; i++) { alert("the index of point: " + index); tempPointArr.push(pointArr[index]); index++; } convertor.translate(tempPointArr, 1, 5, translateCallback); } tempPointArr = []; for(var i = 0; i < remain; i++) { alert("the index of point: " + index); tempPointArr.push(pointArr[index]); index++; } convertor.translate(tempPointArr, 1, 5, translateCallback); }, 1000);
时间: 2024-10-25 12:00:17