坐标批量转换成搜狗坐标

坐标批量转换功能,是将百度坐标,谷歌坐标,以及标准经纬度批量转换成搜狗坐标。此例是将多个百度坐标批量转换成搜狗坐标,然后标记到地图上。

关键代码

function callback(a){
	var features=[];
	 if(a && a.length>0){
		for(var i=0;i<a.length;i++){
			features.push(new sogou.maps.Marker({map:map,position:a[i]}));
		} 
	 }
	 var bound=new sogou.maps.Bounds();
	for(var i=0;i<features.length;i++){
		bound.extend(features[i].getPosition());
	}
	map.fitBounds(bound);

};
 var points=[//百度坐标
		new sogou.maps.Point(116.178633,40.018519),
		new sogou.maps.Point(116.26487,39.971645),
		new sogou.maps.Point(116.381003,39.888431),
		new sogou.maps.Point(116.561527,39.950409),
		new sogou.maps.Point(116.640865,39.827284),
		new sogou.maps.Point(116.507485,39.807776),
		new sogou.maps.Point(116.245323,39.808663),
		new sogou.maps.Point(116.152187,39.922083),
		new sogou.maps.Point(116.192431,39.884888),
		new sogou.maps.Point(116.413199,39.969876),
		new sogou.maps.Point(116.387902,39.810436),
		new sogou.maps.Point(116.490237,39.901717),
		new sogou.maps.Point(116.303965,39.955719),
		new sogou.maps.Point(116.297066,39.901717),
		new sogou.maps.Point(116.33616,39.854763),
		new sogou.maps.Point(116.620168,39.789149),
		new sogou.maps.Point(116.67651,39.772293),
		new sogou.maps.Point(116.843235,39.948639),
		new sogou.maps.Point(117.158289,39.844127)
	];
 var type=3; //type=1 标准经纬度 type=2 google偏转经纬度 type=3 baidu偏转经纬度
sogou.maps.Convertor.translate(points,type,callback);
			

代码解析

首先要指定要转换的坐标的信息,以及坐标的类型。调用sogou.maps.Convertor.translate()方法。

 var points=[//百度坐标
		new sogou.maps.Point(116.178633,40.018519),
		new sogou.maps.Point(116.26487,39.971645),
		new sogou.maps.Point(116.381003,39.888431),
		new sogou.maps.Point(116.561527,39.950409),
		new sogou.maps.Point(116.640865,39.827284),
		new sogou.maps.Point(116.507485,39.807776),
		new sogou.maps.Point(116.245323,39.808663),
		new sogou.maps.Point(116.152187,39.922083),
		new sogou.maps.Point(116.192431,39.884888),
		new sogou.maps.Point(116.413199,39.969876),
		new sogou.maps.Point(116.387902,39.810436),
		new sogou.maps.Point(116.490237,39.901717),
		new sogou.maps.Point(116.303965,39.955719),
		new sogou.maps.Point(116.297066,39.901717),
		new sogou.maps.Point(116.33616,39.854763),
		new sogou.maps.Point(116.620168,39.789149),
		new sogou.maps.Point(116.67651,39.772293),
		new sogou.maps.Point(116.843235,39.948639),
		new sogou.maps.Point(117.158289,39.844127)
	];
 var type=3; //type=1 标准经纬度 type=2 google偏转经纬度 type=3 baidu偏转经纬度
sogou.maps.Convertor.translate(points,type,callback);
			

接下来将新的坐标的结果返回给回调函数,以标记的方式显示到地图上。

function callback(a){
	var features=[];
	 if(a && a.length>0){
		for(var i=0;i<a.length;i++){
			features.push(new sogou.maps.Marker({map:map,position:a[i]}));
		} 
	 }
	 var bound=new sogou.maps.Bounds();
	for(var i=0;i<features.length;i++){
		bound.extend(features[i].getPosition());
	}
	map.fitBounds(bound);

};
			

完整代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>坐标批量转换成搜狗坐标</title>
<style type="text/css">
 #map{position:absolute;width:400px; height:300px;}
</style>
<script type="text/javascript" src="//api.go2map.com/maps/js/api_v2.5.1.js"></script>
</head>

<body>
    <div id="map" style="float:left"></div>
	<div id="result" style="margin-left:400px;font-size:12px;width:550px;">

	</div>
</body>
<script>
/**
*将百度坐标批量转换成搜狗坐标
*/
var myLatLng = new sogou.maps.Point(12957062,4827187);
var myOptions = {
  zoom: 10,
  center: myLatLng
};
var map = new sogou.maps.Map(document.getElementById("map"), myOptions);

function callback(a){
	var features=[];
	 if(a&&a.length>0){
		for(var i=0;i<a.length;i++){
			features.push(new sogou.maps.Marker({map:map,position:a[i]}));
		} 
	 }
	 var bound=new sogou.maps.Bounds();
	for(var i=0;i<features.length;i++){
		bound.extend(features[i].getPosition());
	}
	map.fitBounds(bound);

};
 var points=[//百度坐标
		new sogou.maps.Point(116.178633,40.018519),
		new sogou.maps.Point(116.26487,39.971645),
		new sogou.maps.Point(116.381003,39.888431),
		new sogou.maps.Point(116.561527,39.950409),
		new sogou.maps.Point(116.640865,39.827284),
		new sogou.maps.Point(116.507485,39.807776),
		new sogou.maps.Point(116.245323,39.808663),
		new sogou.maps.Point(116.152187,39.922083),
		new sogou.maps.Point(116.192431,39.884888),
		new sogou.maps.Point(116.413199,39.969876),
		new sogou.maps.Point(116.387902,39.810436),
		new sogou.maps.Point(116.490237,39.901717),
		new sogou.maps.Point(116.303965,39.955719),
		new sogou.maps.Point(116.297066,39.901717),
		new sogou.maps.Point(116.33616,39.854763),
		new sogou.maps.Point(116.620168,39.789149),
		new sogou.maps.Point(116.67651,39.772293),
		new sogou.maps.Point(116.843235,39.948639),
		new sogou.maps.Point(117.158289,39.844127)
	];
 var type=3; //type=1 标准经纬度 type=2 google偏转经纬度 type=3 baidu偏转经纬度
sogou.maps.Convertor.translate(points,type,callback);
</script>
</html>


			

运行代码