百度地图 坐标偏移、覆盖物偏移解决方案

百度地图定位不准确,这个问题困扰我一整天,想来度娘是强大的,各种查询总于找到解决方案,其实就是将经纬度转换为百度地图识别的经纬度,是不是很扯。

网址

http://developer.baidu.com/map/jsdemo/demo/convertor.js     这是单个坐标转换接口

http://developer.baidu.com/map/jsdemo/demo/changeMore.js   这是批量转换接口


1,单个转换函数


//2011-7-25  
(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, 批量转换函数


//2011-7-25 zhangying  
(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 transMore(points,type,callback){  
    var xyUrl = "http://api.map.baidu.com/ag/coord/convert?from=" + type + "&to=4&mode=1";  
    var xs = [];  
    var ys = [];  
    var maxCnt = 20;//每次发送的最大个数  
    var send = function(){  
        var url = xyUrl + "&x=" + xs.join(",") + "&y=" + ys.join(",") + "&callback=callback";  
        //动态创建script标签  
        load_script(url);  
        xs = [];  
        ys = [];  
    }  
    for(var index in points){  
        if(index % maxCnt == 0 && index != 0){  
            send();  
        }  
        xs.push(points[index].lng);  
        ys.push(points[index].lat);  
        if(index == points.length - 1){  
            send();  
        }  
    }  
      
}  
  
window.BMap = window.BMap || {};  
BMap.Convertor = {};  
BMap.Convertor.transMore = transMore;  
})();  

3,单个转换函数与批量转换函数 整合体


(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);  
    };  
}  
  
function transMore(points,type,callback){//批量转换  
    var xyUrl = "http://api.map.baidu.com/ag/coord/convert?from=" + type + "&to=4&mode=1";  
    var xs = [];  
    var ys = [];  
    var maxCnt = 30;//每次发送的最大个数  
    var send = function(){  
        var url = xyUrl + "&x=" + xs.join(",") + "&y=" + ys.join(",") + "&callback=callback";  
        //动态创建script标签  
        load_script(url);  
        xs = [];  
        ys = [];  
    };  
    for(var index in points){  
        if(index % maxCnt == 0 && index != 0){  
            send();  
        }  
        xs.push(points[index].lng);  
        ys.push(points[index].lat);  
        if(index == points.length - 1){  
            send();  
        }  
    }  
      
}  
  
window.BMap = window.BMap || {};  
BMap.Convertor = {};  
BMap.Convertor.translate = translate;  
BMap.Convertor.transMore = transMore;  
})(); 


另外附上两个接口使用的示例源代码

单个转换的接口

[javascript]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
  6. <style type="text/css">  
  7. body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}  
  8. </style>  
  9. <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>  
  10. <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>  
  11. <title>GPS转百度</title>  
  12. </head>  
  13. <body>  
  14. <div id="allmap"></div>  
  15. </body>  
  16. </html>  
  17. <script type="text/javascript">  
  18.   
  19. // 百度地图API功能  
  20. //GPS坐标  
  21. var xx = 116.397428;  
  22. var yy = 39.90923;  
  23. var gpsPoint = new BMap.Point(xx,yy);  
  24.   
  25. //地图初始化  
  26. var bm = new BMap.Map("allmap");  
  27. bm.centerAndZoom(gpsPoint, 15);  
  28. bm.addControl(new BMap.NavigationControl());  
  29.   
  30. //添加谷歌marker和label  
  31. var markergps = new BMap.Marker(gpsPoint);  
  32. bm.addOverlay(markergps); //添加GPS标注  
  33. var labelgps = new BMap.Label("我是GPS标注哦",{offset:new BMap.Size(20,-10)});  
  34. markergps.setLabel(labelgps); //添加GPS标注  
  35.   
  36. //坐标转换完之后的回调函数  
  37. translateCallback = function (point){  
  38.     var marker = new BMap.Marker(point);  
  39.     bm.addOverlay(marker);  
  40.     var label = new BMap.Label("我是百度标注哦",{offset:new BMap.Size(20,-10)});  
  41.     marker.setLabel(label); //添加百度label  
  42.     bm.setCenter(point);  
  43.     alert(point.lng + "," + point.lat);  
  44. }  
  45.   
  46. setTimeout(function(){  
  47.     BMap.Convertor.translate(gpsPoint,0,translateCallback);     //真实经纬度转成百度坐标  
  48. }, 2000);  
  49. </script>  


批量转换的示例接口

[javascript]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
  6. <style type="text/css">  
  7. body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}  
  8. </style>  
  9. <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>  
  10. <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/changeMore.js"></script>  
  11. <title>批量坐标转换</title>  
  12. </head>  
  13. <body>  
  14. <div id="allmap"></div>  
  15. </body>  
  16. </html>  
  17. <script type="text/javascript">  
  18.   
  19. // 百度地图API功能  
  20. var map = new BMap.Map("allmap");  
  21. map.centerAndZoom(new BMap.Point(116.404, 39.915), 15);  
  22. var i;  
  23. var markers = [];  
  24. map.addEventListener("click",function(e){  
  25.     alert(e.point.lng + "," + e.point.lat);  
  26. });  
  27. //注意:百度和谷歌的经纬度坐标顺序是相反的。  
  28. var points = [new BMap.Point(116.3786889372559,39.90762965106183),  
  29.               new BMap.Point(116.38632786853032,39.90795884517671),  
  30.               new BMap.Point(116.39534009082035,39.907432133833574),  
  31.               new BMap.Point(116.40624058825688,39.90789300648029),  
  32.               new BMap.Point(116.41413701159672,39.90795884517671)  
  33.              ];  
  34. function callback(xyResults){  
  35.  var xyResult = null;  
  36.  for(var index in xyResults){  
  37.   xyResult = xyResults[index];  
  38.   if(xyResult.error != 0){continue;}//出错就直接返回;  
  39.   var point = new BMap.Point(xyResult.x, xyResult.y);  
  40.      var marker = new BMap.Marker(point);  
  41.      map.addOverlay(marker);  
  42.      map.setCenter(point);// 由于写了这句,每一个被设置的点都是中心点的过程  
  43.     }  
  44. }  
  45.   
  46. setTimeout(function(){  
  47.     BMap.Convertor.transMore(points,2,callback);        //一秒之后开始进行坐标转换。参数2,表示是从GCJ-02坐标到百度坐标。参数0,表示是从GPS到百度坐标  
  48. }, 1000);  
  49. </script>  
本文转载地址: 点击打开链接




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值