坐标转换、计算距离、计算范围

此示例展示的是如何坐标转换、计算距离、计算范围

关键代码

//获取类的唯一示例
  function getInstance(a) {
    a.hasOwnProperty("_instance")||(a._instance = new a);
    return a._instance
  }
  function initialize() {
    var myLatlng = new sogou.maps.LatLng(39.992792,116.326142);
	var myPoint = new sogou.maps.Point(12956000,4824875);
    var myOptions = {
      zoom: 12,
      center: new sogou.maps.LatLng(39.949945,116.363407),
      mapTypeId: sogou.maps.MapTypeId.ROADMAP
    }
    var map = new sogou.maps.Map(document.getElementById("map_canvas"), myOptions);
	var marker1=new sogou.maps.Marker({
        position: myLatlng, 
        map: map,
		title:"经纬度坐标:"+myLatlng,
		label:{visible:true,align:"TOP"}
    });	   
	var marker2=new sogou.maps.Marker({
        position: myPoint, 
        map: map,
		title:"搜狗坐标:"+myPoint,
		label:{visible:true,align:"BOTTOM"}
    });
	
	//Convertor只需创建一个唯一的对象即可。
	var convertor=getInstance(sogou.maps.Convertor);
	//标准经纬度转换为Sogou地图坐标
	var sogouCoord=convertor.toSogou(myLatlng);
	alert("经纬度="+myLatlng+",搜狗坐标="+sogouCoord);
	var bounds=convertor.bounds(sogouCoord,2000);
	alert("以搜狗坐标="+sogouCoord+"为中心,2000为半径的区域范围="+bounds);
	var distance=convertor.distance(sogouCoord,myPoint);
	alert(myLatlng+"与"+myPoint+"之间的距离为"+parseInt(distance)+"米");
	var line=new sogou.maps.Polyline({
      path: [myLatlng,myPoint],
      strokeColor: "#FF0000",
      strokeOpacity: 1.0,
      strokeWeight: 5,
	  title:parseInt(distance)+"米",
	  map:map
    });
	
  }
			

代码解析

在创建地图时,用标准经纬度设置地图中心点。

var myOptions = {
      zoom: 12,
      center: new sogou.maps.LatLng(39.949945,116.363407),
      mapTypeId: sogou.maps.MapTypeId.ROADMAP
    }
var map = new sogou.maps.Map(document.getElementById("map_canvas"), myOptions);
			

分别用搜狗坐标与标准经纬度坐标添加标记

var myLatlng = new sogou.maps.LatLng(39.992792,116.326142);
var myPoint = new sogou.maps.Point(12956000,4824875);
var marker1=new sogou.maps.Marker({
	position: myLatlng, 
	map: map,
	title:"经纬度坐标:"+myLatlng,
	label:{visible:true,align:"TOP"}
});	   
var marker2=new sogou.maps.Marker({
	position: myPoint, 
	map: map,
	title:"搜狗坐标:"+myPoint,
	label:{visible:true,align:"BOTTOM"}
});

创建一个唯一的Convertor对象

var convertor=getInstance(sogou.maps.Convertor);

标准经纬度转换为Sogou地图坐标,并输出

var sogouCoord=convertor.toSogou(myLatlng);
alert("经纬度="+myLatlng+",搜狗坐标="+sogouCoord);	
	

获取以搜狗坐标为中心的半径为2000米的范围,并输出。

var bounds=convertor.bounds(sogouCoord,2000);
alert("以搜狗坐标="+sogouCoord+"为中心,2000为半径的区域范围="+bounds);
			

获取两点之间的距离并输出。

var distance=convertor.distance(sogouCoord,myPoint);
alert(myLatlng+"与"+myPoint+"之间的距离为"+parseInt(distance)+"米");
			

在两点之间添加一条线段。并显示距离。

var line=new sogou.maps.Polyline({
      path: [myLatlng,myPoint],
      strokeColor: "#FF0000",
      strokeOpacity: 1.0,
      strokeWeight: 5,
	  title:parseInt(distance)+"米",
	  map:map
    });
			

完整代码

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<style type="text/css">
html {height: auto;}
body {height: auto;margin: 0;padding: 0;}
#map_canvas {width:1000px;height: 500px;position: absolute;}
@media print {#map_canvas {height: 950px;}}
</style>
<title>搜狗地图 JavaScript API 示例:坐标转换、计算距离、计算范围</title>
<script type="text/javascript" src="//api.go2map.com/maps/js/api_v2.5.1.js"></script>
<script type="text/javascript">
  //获取类的唯一示例
  function getInstance(a) {
    a.hasOwnProperty("_instance")||(a._instance = new a);
    return a._instance
  }
  function initialize() {
    var myLatlng = new sogou.maps.LatLng(39.992792,116.326142);
	var myPoint = new sogou.maps.Point(12956000,4824875);
    var myOptions = {
      zoom: 12,
      center: new sogou.maps.LatLng(39.949945,116.363407),
      mapTypeId: sogou.maps.MapTypeId.ROADMAP
    }
    var map = new sogou.maps.Map(document.getElementById("map_canvas"), myOptions);
	var marker1=new sogou.maps.Marker({
        position: myLatlng, 
        map: map,
		title:"经纬度坐标:"+myLatlng,
		label:{visible:true,align:"TOP"}
    });	   
	var marker2=new sogou.maps.Marker({
        position: myPoint, 
        map: map,
		title:"搜狗坐标:"+myPoint,
		label:{visible:true,align:"BOTTOM"}
    });
	
	//Convertor只需创建一个唯一的对象即可。
	var convertor=getInstance(sogou.maps.Convertor);
	//标准经纬度转换为Sogou地图坐标
	var sogouCoord=convertor.toSogou(myLatlng);
	alert("经纬度="+myLatlng+",搜狗坐标="+sogouCoord);
	var bounds=convertor.bounds(sogouCoord,2000);
	alert("以搜狗坐标="+sogouCoord+"为中心,2000为半径的区域范围="+bounds);
	var distance=convertor.distance(sogouCoord,myPoint);
	alert(myLatlng+"与"+myPoint+"之间的距离为"+parseInt(distance)+"米");
	var line=new sogou.maps.Polyline({
      path: [myLatlng,myPoint],
      strokeColor: "#FF0000",
      strokeOpacity: 1.0,
      strokeWeight: 5,
	  title:parseInt(distance)+"米",
	  map:map
    });
	
  }
</script>
</head>
<body onload="initialize()">
  <div id="map_canvas"></div>
</body>
</html>
			

运行代码