window.onload = function () { //初始化地图 map = new sogou.maps.Map(document.getElementById("map_canvas"), {}); //为地图添加点击事件 sogou.maps.event.addListener(map, 'click', function (event) { var marker1 = new sogou.maps.Marker({ position: event.point, map: map }); }); }
根据两描点测距
//获取类的唯一示例 function getInstance(a) { a.hasOwnProperty("_instance") || (a._instance = new a); return a._instance } //两点相连 function Lines(myLatlng, myPoint) { var convertor = getInstance(sogou.maps.Convertor); var distance = convertor.distance(myLatlng, myPoint); //两点链接 var line = new sogou.maps.Polyline({ path: [myLatlng, myPoint], strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 1, title: parseInt(distance) + "米", map: map }); }
根据上述属性做了一个小的模块,地图上动态测距代码如下:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <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> <script src="https://xiazai.jb51.net/201409/other/api_v2.5.1.js" type="text/javascript"></script> <script> var map;var num;var Listener; //获取类的唯一示例 function getInstance(a) { a.hasOwnProperty("_instance") || (a._instance = new a); return a._instance } window.onload = function () { //初始化地图 map = new sogou.maps.Map(document.getElementById("map_canvas"), {}); } function AddCj() { var mypointh; var myPoint; num = 0; //为地图添加点击事件、点击后显示当前坐标并添加点击描点 Listener = sogou.maps.event.addListener(map, 'click', function (event) { if (num == 0) { mypointh = myPoint = event.point; //获取点击位置的坐标 } else { myPoint = mypointh; mypointh = event.point; //获取点击位置的坐标 } Lines(mypointh, myPoint); num++; }); } function DelCj() { sogou.maps.event.removeListener(Listener) } //两点相连 function Lines(myLatlng, myPoint) { var convertor = getInstance(sogou.maps.Convertor); var distance = convertor.distance(myLatlng, myPoint); //两点链接 var line = new sogou.maps.Polyline({ path: [myLatlng, myPoint], strokeColor: "#FF0000", strokeOpacity: 1.0, strokeWeight: 1, title: parseInt(distance) + "米", map: map }); placeMarker(myLatlng, parseInt(distance)); } //动态添加描点,根据指定的坐标创建描点 function placeMarker(location,jl) { var clickedLocation = location; var marker1 = new sogou.maps.Marker({ position: location, title: jl+"米", label:{visible:true,align:"BOTTOM"}, map: map }); } function Mapclear() { num = 0; map.clearAll(); } </script> </head> <body> <form runat="server"> <input type="button" value="测距" /> <input type="button" value="取消测距" /> <input type="button" value="清空" /> <div ></div> </form> </body> </html>