百度地图API如何批量转换为百度经纬度

开发 前端
百度地图API的官网上提供了常用坐标转换的示例。但是,一次只能转换一个,真的非常麻烦!!这里结合了官方的示例,自制一个批量转换工具,供大家参考。

  百度地图API的官网上提供了常用坐标转换的示例。但是,一次只能转换一个,真的非常麻烦!!这里结合了官方的示例,自制一个批量转换工具,供大家参考。

因为我没有GPS坐标,就拿谷歌坐标做个示例了。

  首先要注意的是,百度和谷歌的经纬度坐标顺序是相反的。

  比如,谷歌的经纬度是

  newgoogle.maps.LatLng(39.90762965106183, 116.3786889372559)

  传入坐标转换接口的百度经纬度应该是

  newBMap.Point(116.3786889372559,39.90762965106183)

  所以,我建立一个数组,存放转换前的经纬度。创建百度的坐标点,但是用谷歌的经纬度。

  1.   //注意:百度和谷歌的经纬度坐标顺序是相反的。  
  2.   varpoints = [newBMap.Point(116.3786889372559,39.90762965106183),  
  3.   newBMap.Point(116.38632786853032,39.90795884517671),  
  4.   newBMap.Point(116.39534009082035,39.907432133833574),  
  5.   newBMap.Point(116.40624058825688,39.90789300648029),  
  6.   newBMap.Point(116.41413701159672,39.90795884517671)  
  7.   ]; 

  然后调用官方公布的接口

  BMap.Convertor.transMore(points,2,callback);

  自己对这个坐标转换接口做了修改,让它可以多次返回结果。注意看注释部分。

  据说,百度坐标转换接口,有50次/秒的限制。

  1.   functiontransMore(points,type,callback){  
  2.   for(varindex inpoints){  
  3.   if(index >50){return;}  
  4.   varxyUrl = "http://api.map.baidu.com/ag/coord/convert?from=" + type +  
  5.   "&to=4&x=" + points[index].lng + //这里要循环读入数组points的lng数据,直到points.length完毕。  
  6.   "&y=" + points[index].lat +  
  7.   "&callback=callback";  
  8.   //动态创建script标签  
  9.   load_script(xyUrl);  
  10.   }  
  11.  } 

  进过上一步,坐标就转换好了。成为百度坐标了。但这时的百度坐标是加密的。看不懂……

  好在,我们可以直接利用这些加密的编码创建出Marker标注点。获取到对象后,直接使用即可。

  1.   functioncallback(xyResult){  
  2.   if(xyResult.error != 0){return;}//出错就直接返回;  
  3.   varpoint = newBMap.Point(xyResult.x, xyResult.y);  
  4.   varmarker = newBMap.Marker(point);  
  5.   map.addOverlay(marker);  
  6.   map.setCenter(point);//由于写了这句,可以每一个被转的点都是中心点的过程  
  7.   } 

  到这里,批量转换就讲完啦~~

  下面说说我自己添加的其他功能:如何获取地图上的坐标点。

  如何获取地图上的坐标点,经纬度?

  先说说谷歌的:给地图添加事件,点击地图后直接弹出。

  1.   google.maps.event.addListener(map, 'click', function(e) {  
  2.   alert(e.latLng);  
  3.   }); 

  在说说百度的,也是给地图添加事件。

  1.   map.addEventListener("click",function(e){  
  2.   alert(e.point.lng + "," + e.point.lat);  
  3.   }); 

  大家发现谷歌和百度有什么不同了没有?

  对了,谷歌的经纬度像是封装在一起了样。而百度的经纬度是分开地~~~

  全部源代码:

  有两个文件,一个是htm,另一个是修改后的官方坐标转换js。

  批量转换.htm

  1.   <!DOCTYPE html> 
  2.   <html> 
  3.   <head> 
  4.   <meta http-equiv="Content-Type"content="text/html; charset=gb2312"/> 
  5.   <script type="text/javascript"src="changeMore.js"></script> 
  6. <title>批量转换坐标</title> 
  7.   </head> 
  8.   <body> 
  9.   <input onclick="magic();"value="批量转换"type="button"/>(据说有50次/秒的限制哦)<hr /> 
  10.   <div style="clear:both"> 
  11.   <div style="float:left;"> 
  12.   <h4>谷歌地图</h4> 
  13.   <div style="width:520px;height:340px;border:1px solid gray"id="map_canvas"></div> 
  14.   <p>鼠标点击的谷歌坐标是:<span id="info"></span></p> 
  15.   <script type="text/javascript"src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
  16. <script type="text/javascript"> 
  17.   functioninitialize() {varmyOptions ={  
  18.   zoom: 14,  
  19.   center: newgoogle.maps.LatLng(39.90861722866082, 116.39679921252446),  
  20.   mapTypeId: google.maps.MapTypeId.ROADMAP  
  21.   };varmap =newgoogle.maps.Map(document.getElementById('map_canvas'),myOptions);  
  22.   google.maps.event.addListener(map, 'click', function(e) {  
  23.   document.getElementById("info").innerHTML =e.latLng;  
  24.   });varmarker1 =newgoogle.maps.Marker({  
  25.   position: newgoogle.maps.LatLng(39.90762965106183, 116.3786889372559),  
  26.   map: map  
  27.   });varmarker2 =newgoogle.maps.Marker({  
  28.   position: newgoogle.maps.LatLng(39.90795884517671, 116.38632786853032),  
  29.   map: map  
  30.   });varmarker3 =newgoogle.maps.Marker({  
  31.   position: newgoogle.maps.LatLng(39.907432133833574, 116.39534009082035),  
  32.   map: map  
  33.   });varmarker4 =newgoogle.maps.Marker({  
  34.   position: newgoogle.maps.LatLng(39.90789300648029, 116.40624058825688),  
  35.   map: map  
  36.   });varmarker5 =newgoogle.maps.Marker({  
  37.   position: newgoogle.maps.LatLng(39.90795884517671, 116.41413701159672),  
  38.   map: map  
  39.   });  
  40.   }  
  41.   google.maps.event.addDomListener(window, 'load', initialize);</script> 
  42.   </div> 
  43.   <div style="float:left;"> 
  44.   <h4>百度地图</h4> 
  45.   <div style="width:520px;height:340px;border:1px solid gray"id="container"></div> 
  46.   <p>鼠标点击的百度坐标是:(<span id="info2"></span></p> 
  47.   <script type="text/javascript"src="http://api.map.baidu.com/api?v=1.2"></script> 
  48. <script type="text/javascript"> 
  49.   varmap =newBMap.Map("container");  
  50.   map.centerAndZoom(newBMap.Point(116.404, 39.915), 15);vari;varmarkers =[];  
  51.   map.addEventListener("click",function(e){  
  52.   document.getElementById("info2").innerHTML =e.point.lng +","+e.point.lat;  
  53.   });//注意:百度和谷歌的经纬度坐标顺序是相反的。  
  54.   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)  
  55.   ];functioncallback(xyResult){ if(xyResult.error !=0){return;}//出错就直接返回;varpoint =newBMap.Point(xyResult.x, xyResult.y);varmarker =newBMap.Marker(point);  
  56.   map.addOverlay(marker);  
  57.   map.setCenter(point);//由于写了这句,可以每一个被转的点都是中心点的过程  
  58.   }functionmagic(){  
  59.   BMap.Convertor.transMore(points,2,callback);  
  60.   }</script> 
  61.   </div> 
  62.   </div> 
  63.   </body> 
  64.   </html> 
  65.   changeMore.js  
  66.   //2011-7-25 zhangying  
  67.   (function(){  
  68.   functionload_script(xyUrl, callback){  
  69.   varhead = document.getElementsByTagName('head')[0];  
  70.  varscript = document.createElement('script');  
  71.   script.type = 'text/javascript';  
  72.   script.src = xyUrl;  
  73.   //借鉴了jQuery的script跨域方法  
  74.   scriptscript.onload = script.onreadystatechange = function(){  
  75.   if((!this.readyState || this.readyState === "loaded" || this.readyState === "complete")){  
  76.   callback &&callback();  
  77.   //Handle memory leak in IE  
  78.   scriptscript.onload = script.onreadystatechange = null;  
  79.   if( head &&script.parentNode ) {  
  80.   head.removeChild( script );  
  81.   }  
  82.   }  
  83.   };  
  84.   //Use insertBefore instead of appendChild to circumvent an IE6 bug.  
  85.   head.insertBefore( script, head.firstChild );  
  86.   }  
  87.   functiontransMore(points,type,callback){  
  88.   for(varindex inpoints){  
  89.   if(index >50){return;}  
  90.   varxyUrl = "http://api.map.baidu.com/ag/coord/convert?from=" + type +  
  91.   "&to=4&x=" + points[index].lng + //这里要循环读入数组points的lng数据,直到points.length完毕。  
  92.   "&y=" + points[index].lat +  
  93.   "&callbackcallback=callback";  
  94.   //动态创建script标签  
  95.   load_script(xyUrl);  
  96.   }  
  97.   }  
  98.   windowwindow.BMap = window.BMap || {};  
  99.   BMap.Convertor = {};  
  100.   BMap.Convertor.transMore = transMore;  
  101.   })(); 

原文链接:http://www.cnblogs.com/milkmap/archive/2011/09/29/2195780.html

【编辑推荐】

  1. 详解百度地图API之地图标注
  2. 百度地图API之如何制作驾车导航
  3. 详解百度地图API之地图操作
  4. 详解百度地图API之自定义地图类型
  5. 怎么成为一个软件架构师
责任编辑:彭凡 来源: 博客园
相关推荐

2011-09-29 11:00:54

百度地图API

2011-10-21 10:16:25

百度地图API

2011-09-16 14:39:02

百度地图API

2011-10-24 14:01:29

API

2011-09-16 10:37:42

地图API

2011-09-26 10:05:19

百度地图API

2012-02-01 09:33:36

百度地图API

2013-08-22 17:08:50

2014-07-25 17:12:39

数据库WOT2014MongoDB

2011-10-09 11:07:40

百度地图API

2013-04-08 14:59:54

Android学习笔记百度地图Overlay

2012-05-28 22:51:53

百度

2018-09-06 18:37:45

百度云

2011-10-21 09:11:41

百度地图API

2021-06-15 14:33:00

高德百度腾讯

2020-12-03 06:13:46

iOS

2016-03-25 11:18:23

中华网

2013-06-27 10:23:30

百度云百度开放云

2012-10-19 09:47:30

百度云百度音乐云计算

2014-09-04 02:25:24

百度世界大会2014直达号BaiduEye
点赞
收藏

51CTO技术栈公众号