创建测距离的接口对象时只能传递两个点进去为了再页面上方便展示测距效果,先画线,再将线上的两个端点当作参数创建距离对象。最后将生成的距离通过创建的标签放到线段上展示
上代码:
// 距离
function getDistance() {
//绑定事件
sgworld.Window.SetInputMode(1); //鼠标样式修改
sgworld.AttachEvent("OnLButtonDown", StartDrawLine1);
sgworld.AttachEvent("OnFrame", DrawLineOnFrame);
sgworld.AttachEvent("OnRButtonUp", StopDrawDistance)
}
function StartDrawLine1(flags, x, y) {
// flags:虚拟键按下的标记
// x、y为屏幕坐标
var lineColor = sgworld.Creator.CreateColor(255, 100, 0, 125); // 定义线渲染样式
var pWorldPointInfo = sgworld.Window.PixelToWorld(x, y, -1); // 将屏幕坐标转换为地理坐标
var pos = pWorldPointInfo.Position
if (null == pWorldPointInfo) {
return false;
}
var pos = pWorldPointInfo.Position;
// alert(pos)
if (!polyLine) {
var geometry = sgworld.Creator.GeometryCreator.CreateLineStringGeometry([pos, pos]);
polyLine = sgworld.Creator.CreatePolyline(geometry, lineColor, 2, sgworld.ProjectTree.RootID, "");
polyLine.Geometry.StartEdit(); //开始编辑线
} else {
polyLine.Geometry.Points.AddPoint(pos.X, pos.Y, 0);
var pPoints = polyLine.Geometry.Points;
var point = []
point.push(pPoints.Item(pPoints.count - 1))
point.push(pPoints.Item(1))
var geometry = sgworld.Creator.GeometryCreator.CreateLineStringGeometry(point)
var distance = sgworld.Analysis.MeasureTerrainGroundDistance(geometry, 0, true)
p1 = polyLine.Geometry.Points.Item(polyLine.Geometry.Points.count - 1)
p2 = polyLine.Geometry.Points.Item(polyLine.Geometry.Points.count - 2)
var p3 = sgworld.Creator.CreatePosition()
p3.x = p2.x - p1.x
p3.y = p2.y - p1.y
cLabelStyle = sgworld.Creator.CreateLabelStyle();
alert('所测距离为' + distance)
sgworld.Creator.CreateTextLabel(pos, distance, cLabelStyle, "");
// sgworld.Creator.CreateTextLabel(
// p3,
// distance)
}
}
function StopDrawDistance() {
if (polyLine == null || polyLine.Geometry.Points.count <= 2) {
return false;
}
// 删除最后一个点
polyLine.Geometry.Points.DeletePoint(polyLine.Geometry.Points.count - 1);
// 解除绑定事件
sgworld.DetachEvent("OnLButtonDown", StartDrawLine1);
sgworld.DetachEvent("OnFrame", DrawLineOnFrame);
sgworld.DetachEvent("OnRButtonUp", StopDrawDistance);
// 结束线编辑功能
polyLine.Geometry.EndEdit();
var pPoints = polyLine.Geometry.Points;
var point = []
point.push(pPoints.Item(2))
point.push(pPoints.Item(1))
var geometry = sgworld.Creator.GeometryCreator.CreateLineStringGeometry(point)
var distance = sgworld.Analysis.MeasureTerrainGroundDistance(geometry, 0, true)
// alert('所测距离为' + distance)
polyLine = null;
sgworld.Window.SetInputMode(0); //鼠标恢复效果
// ScanTree()
}