百度地图位置偏差解决及批量转换

工作中遇到的问题,网上搜索解决后整理,简记之。

问题:百度地图展示位置时,与真实位置有偏差;

           大量数据,如轨迹回放时,单个坐标转换耗时太长,需自定义每次请求转换坐标数;

1.百度提供的转化js,实现GPS定位转百度坐标

var point = new BMap.Point(lng, lat);
BMap.Convertor.translate(point,0,callback);  //真实经纬度转成百度坐标

回调函数:

function callback(point) {
	//得到转换后点point
    //do something
}

百度转换js:

(function(){        //闭包
function load_script(xyUrl, callback){
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = xyUrl;
    //借鉴了jQuery的script跨域方法
    script.onload = script.onreadystatechange = function(){
        if((!this.readyState || this.readyState === "loaded" || this.readyState === "complete")){
            callback && callback();
            // Handle memory leak in IE
            script.onload = script.onreadystatechange = null;
            if ( head && script.parentNode ) {
                head.removeChild( script );
            }
        }
    };
    // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
    head.insertBefore( script, head.firstChild );
}
function translate(point,type,callback){
    var callbackName = 'cbk_' + Math.round(Math.random() * 10000);    //随机函数名
    var xyUrl = "http://api.map.baidu.com/ag/coord/convert?from="+ type + "&to=4&x=" + point.lng + "&y=" + point.lat + "&callback=BMap.Convertor." + callbackName;
    //动态创建script标签
    load_script(xyUrl);
    BMap.Convertor[callbackName] = function(xyResult){
        delete BMap.Convertor[callbackName];    //调用完需要删除改函数
        var point = new BMap.Point(xyResult.x, xyResult.y);
        callback && callback(point);
    }
}

window.BMap = window.BMap || {};
BMap.Convertor = {};
BMap.Convertor.translate = translate;
})();

2.改写百度批量转换js,实现坐标批量转换,利用递归回调控制坐标转换的回传顺序,保证先请求坐标先获取转换结果

获取坐标点:

利用ajax获取数据,将数据存放在pointsArray数组中;

            //封装GPS坐标
			function wareGpsPointsBeforeSend(gpsPoints){
				var pointsArray=new Array();
				var times=Math.floor(gpsPoints.length/maxCnt);//maxCnt 每次请求数据量
				var k=0;
				for(var i=0;i<times;i++){
					pointsArray[i]=new Array();
					for(var j=0;j<maxCnt;j++,k++){
						pointsArray[i][j]=gpsPoints[k];
					}
				}
				if(k<gpsPoints.length){
					var j=0,i=times;
					pointsArray[i]=new Array();
					while(k<gpsPoints.length){
						pointsArray[i][j]=gpsPoints[k];
						k++;
						j++;
					}
				}
				return pointsArray;
			}

请求转换方法:

var pointIndex = 0;//递归计数
function transGPS(){
				BMap.Convertor.myTransMore(pointsArray[pointIndex],0,translateCallback); //真实经纬度转成百度坐标
			}

回调函数:

function translateCallback(data){
				console.log(data);
				if(data.status!=0){
					alert("转换出错")
					return;
				}
				var transPoints=data.result;
				var point=null;
				for ( var index in transPoints) {
					point=transPoints[index];
                    //points存放转换后坐标点数组
					points.push(new BMap.Point(point.x,point.y));
				}
				pointIndex++;
				if(pointIndex<pointsArray.length){
					transGPS();
				}else{
					console.log(points);
					
				}
			}

修改百度批量上传js:

function myTransMore(points,type,callback){
	var xyUrl = "http://api.map.baidu.com/geoconv/v1/?coords=";
	var coordsStr="";
	var maxCnt = 100;//每次发送的最大个数
	var callbackName = 'cbk_' + Math.round(Math.random() * 10000);    //随机函数名
	var send = function(){
		var positionUrl = xyUrl + coordsStr + "&from=1&to=5&ak=dzocZvw1DPkpjzBuoxtpKtIq9pTmmzx9&callback=BMap.Convertor."+callbackName;
	   
//		console.log(positionUrl);
		load_script(positionUrl);
		BMap.Convertor[callbackName] = function(result){
			 delete BMap.Convertor[callbackName];    //调用完需要删除改函数
			callback && callback(result);
		}
		coordsStr="";
	}
    for(var index in points){
		if(index % maxCnt == 0 && index != 0){
			send();
		}
		coordsStr+=points[index].lng+','+points[index].lat;
		if(index < points.length - 1){
			coordsStr=coordsStr+";";
		}
		if(index == points.length - 1){
			send();
		}
    }
    
}

百度转换及改写的批量转换js文件:https://download.csdn.net/download/f954618482/10674899

 

©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页