skyline二次开发之Analysis操作测距离

创建测距离的接口对象时只能传递两个点进去为了再页面上方便展示测距效果,先画线,再将线上的两个端点当作参数创建距离对象。最后将生成的距离通过创建的标签放到线段上展示
上代码:

// 距离
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()
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值