關於百度地圖API的地圖座標轉換問題

我在以前的文章利用html5獲取經緯度而且在百度地圖中顯示位置中使用了百度地圖的API來顯示html5獲取的地理位置,在文中我說過這樣的話,我說百度地圖的準確度不怎麼精確,誤差很大。這裏我要更正下:javascript

國際經緯度座標標準爲WGS-84,國內必須至少使用國測局制定的GCJ-02,對地理位置進行首次加密。百度座標在此基礎上,進行了BD-09二次加密措施,更加保護了我的隱私。百度對外接口的座標系並非GPS採集的真實經緯度,須要經過座標轉換接口進行轉換。html

由此能夠看出小編以前冤枉了百度地圖,因此若是對您有誤導還請見諒。因此寫了篇關於百度地圖API座標轉換的文章,而且對以前的模型作了修正。html5

實現代碼:java

1 //在百度 map中顯示地址
2     var map = new BMap.Map("map_canvas");
3     var point = new BMap.Point(longitudeP , latitudeP);  // 建立點座標  
4     map.centerAndZoom(point, 15);// 初始化地圖,設置中心點座標和地圖級別  
5     var marker = new BMap.Marker(point);    
6     map.addOverlay(marker); 
7     BMap.Convertor.translate(point,0,translateCallback);     //真實經緯度轉成百度座標

回調函數代碼:git

1 //座標轉換完以後的回調函數
2 function translateCallback(point1){
3     var marker1 = new BMap.Marker(point1);
4     map.addOverlay(marker1);
5     var label = new BMap.Label("轉換後的百度座標",{offset:new BMap.Size(20,-10)});
6     marker1.setLabel(label); //添加百度label
7     map.setCenter(point1);
8 }

這是新的效果圖:canvas

能夠看出轉換出的效果仍是至關精確的,而且從我這裏的demo來看要比谷歌地圖精確的多!因此若是也有像我同樣的用戶發現調用baidu map座標有誤差的話,頗有可能就是沒有進行座標轉換。api

關於這個demo:

在線查看demo 瀏覽器

 

demo代碼:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
 6 <title>GIS開發利用html5獲取經緯度並在百度地圖中查看</title>
 7 <!--加載百度 map api-->
 8 <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=8827ee39511568ac0705d037d67b2624"></script>  
 9 <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
10 </head>
11 
12 <body>
13 <span id="support">將下面的經緯度輸入谷歌地圖:</span>
14 <div id="show">
15 緯度:<span id="latitude"></span><br />
16 經度:<span id="longitude"></span><br />
17 準確度:<span id="accuracy"></span>
18 </div>
19 <div id="map_canvas" style="width:500px; height:500px;"></div>
20 <script type="text/javascript">
21 var doc = document,
22     latitude = doc.getElementById('latitude'),
23     longitude = doc.getElementById('longitude'),
24     accuracy = doc.getElementById('accuracy'),
25     support = doc.getElementById('support'),
26     showDiv = doc.getElementById('show');
27 var map = new BMap.Map("map_canvas");
28 function lodeSupport(){
29     if(navigator.geolocation){
30         support.innerHTML = '將下面的經緯度輸入谷歌地圖(緯度 經度)查看本身位置:';
31         showDiv.style.display = 'block';
32         navigator.geolocation.getCurrentPosition(updataPosition);
33     }else{
34         support.innerHTML = '對不起,瀏覽器不支持!';
35         showDiv.style.display = 'none';
36     }
37 }
38 function updataPosition(position){
39     var latitudeP = position.coords.latitude,
40         longitudeP = position.coords.longitude,
41         accuracyP = position.coords.accuracy;
42     latitude.innerHTML = latitudeP;
43     longitude.innerHTML = longitudeP;
44     accuracy.innerHTML = accuracyP;
45     //在百度 map中顯示地址
46     
47     var point = new BMap.Point(longitudeP , latitudeP);  // 建立點座標  
48     map.centerAndZoom(point, 15);// 初始化地圖,設置中心點座標和地圖級別  
49     var marker = new BMap.Marker(point);    
50     map.addOverlay(marker); 
51     BMap.Convertor.translate(point,0,translateCallback);     //真實經緯度轉成百度座標
52     
53 }
54 //座標轉換完以後的回調函數
55 function translateCallback(point1){
56     var marker1 = new BMap.Marker(point1);
57     map.addOverlay(marker1);
58     var label = new BMap.Label("轉換後的百度座標",{offset:new BMap.Size(20,-10)});
59     marker1.setLabel(label); //添加百度label
60     map.setCenter(point1);
61 }
62 
63 window.addEventListener('load', lodeSupport , true);
64 </script>
65 </body>
66 </html>

 

 轉載自:http://malagis.com/baidu-maps-api-map-coordinate-conversion.html函數

相關文章
相關標籤/搜索